gpt4 book ai didi

c# - 链接按钮单击事件由于其保护级别而无法访问

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:19 25 4
gpt4 key购买 nike

我正在做一个项目。我正在动态创建一个 asp 表,并在表格单元格中根据条件添加链接按钮。但是在将 Click 事件添加到链接按钮时,它给出了一个错误提示-

System.Web.UI.WebControls.LinkButton.OnClick(System.EventArgs)' is inaccessible due to its protection level

下面是我制作表格的代码

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
setmonthname();
}
makeCalendar();
}
public void makeCalendar()
{
tblcalendar.Rows.Clear();
//for current month
DateTime startingdate = StartDateOfMonth(DateTime.Now.AddMonths(monthclickedno));
DateTime enddate = EndDateOfMonth(DateTime.Now.AddMonths(monthclickedno));
string startingday = startingdate.DayOfWeek.ToString();
int startingdayno = Convert.ToInt32(startingdate.DayOfWeek);
string endday = enddate.DayOfWeek.ToString();//like saturday is 6,stating is from monday with 1 and ending si sunday with 7
int enddayno = Convert.ToInt32(enddate.DayOfWeek);
//for prevoius month
DateTime enddateprevious = (EndDateOfMonth(DateTime.Now.AddMonths(monthclickedno)));
//for next month
DateTime startingdatenext = StartDateOfMonth(DateTime.Now.AddMonths(1));
DateTime dtstart=startingdate.AddDays(-(startingdayno+1));
//sMonthName = "January";
//int iMonthNo = Convert.ToDateTime("01-" + sMonthName + "-2011").Month;
for (int i = 0; i <7;i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 7;j++ )
{
TableCell tc = new TableCell();
clickablecell ctCell = new clickablecell();
//tc.ID = idtc.ToString();
idtc++;
if(i==0)
{
tr.CssClass = "firstrow";
tc.CssClass = "firstrowcell";
if (j == 0)
tc.Text = "Sun";
else if (j == 1)
tc.Text = "Mon";
else if (j == 2)
tc.Text = "Tue";
else if (j == 3)
tc.Text = "Wed";
else if (j == 4)
tc.Text = "Thu";
else if (j == 5)
tc.Text = "Fri";
else if (j == 6)
tc.Text = "Sat";
tr.Cells.Add(tc);
}
else{
tc.CssClass = "othercells";
dtstart=dtstart.AddDays(1);
//if date is single digit like 1,2
if (dtstart.ToString("dd").Substring(0, (dtstart.ToString("dd").Length)-1) == "0")
ctCell.Text = (dtstart.ToString("dd").Substring(1));
else
ctCell.Text = (dtstart.ToString("dd"));
ctCell.Attributes.Add("onmouseover", "defColor=this.style.backgroundColor; this.style.backgroundColor='LightGray';");
ctCell.Attributes.Add("onmouseout", "this.style.backgroundColor=defColor;");
//ctCell.ID = k.ToString();
k++;
ctCell.Click += new clickablecell.ClickEventHandler(textcell_Click);
//check for events in this date
DataTable dtevents = checkEvents(dtstart.ToString("dd-MM-yyyy"));
if (dtevents.Rows.Count != 0)
{
LinkButton lnkevent = new LinkButton();
if (dtevents.Rows.Count == 1)
{
if (dtevents.Rows[0]["eventtype"].ToString() == "Holiday")
{
lnkevent.Text = dtevents.Rows[0]["eventtype"].ToString();
lnkevent.CssClass = "tcholidaytext";
ctCell.CssClass = "tcholidaytext";
}
else if (dtevents.Rows[0]["eventtype"].ToString() == "Event")
{
lnkevent.Text = dtevents.Rows[0]["eventtype"].ToString();
lnkevent.CssClass = "tceventtext";
ctCell.CssClass = "tceventtext";
}
else
{
lnkevent.Text = dtevents.Rows[0]["eventtype"].ToString();
lnkevent.CssClass = "tcimpdaytext";
ctCell.CssClass = "tcimpdaytext";
}
}
else
{
ctCell.CssClass = "tcmixtext";
}
//lnkevent.Attributes.Add("onClick", "test();");
lnkevent.OnClick += new EventHandler(this,test);
ctCell.Controls.Add(lnkevent);
}

tr.Cells.Add(ctCell);
}

tblcalendar.Rows.Add(tr);
}
}
}
public void test(object sender,EventArgs e)
{
Response.Write("helloo");
}

请帮我解决这个问题

最佳答案

正确的订阅应该是这样的:

lnkevent.Click += test;

OnClick 是类内部使用的引发事件的方法。相反,您应该订阅事件本身。

关于c# - 链接按钮单击事件由于其保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23243803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com