2

I am working on a project.I am creating an asp table dynamically and in table cell I am adding link button depending on condition.But while adding the Click event to link button it is giving an error saying-

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

Following is my code of making the table

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");
}

Please help how I can solve this problem

rupinder18
  • 795
  • 1
  • 18
  • 43

1 Answers1

5

Correct subscription should look like this:

lnkevent.Click += test;

OnClick is a method used internally inside the class to raise the event. On the contrary you should be subscribing to the event itself.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • tried the above code now its not giving an error but click event is not being fired – rupinder18 Apr 23 '14 at 12:27
  • @rupinder18, that's a different matter. My bet is that you are not adding these LinkButtons to the page on every postback. Are you? – Andrei Apr 23 '14 at 12:29
  • you can check my code above i am adding the link buttons to the table cell do I need to modify some code while adding the link button please suggest – rupinder18 Apr 23 '14 at 12:31
  • @rupinder18, dynamic controls (those added in code, not in markup, like your linkbuttons) should be added to the page on every postback for their events to work properly. It is not clear from the part of code you have posted whether this is happening in your case. So again the question - are you running this `makeCalendar` method on every postback? – Andrei Apr 23 '14 at 12:36
  • please check above I have pasted my page load method code – rupinder18 Apr 23 '14 at 12:39
  • have you checked please suggest changes required,its really urgent..thanx – rupinder18 Apr 23 '14 at 12:43
  • @rupinder18, not sure. Maybe these LinkButtons end up with different ID on every load, thus ASP.NET is not able to identify them. Try debugging and see if this is the case – Andrei Apr 23 '14 at 12:47
  • but I haven't assigned any id to the link buttons so what should i do to view them and if this is the case as you have told so what solution should I follow – rupinder18 Apr 23 '14 at 12:50
  • @rupinder18, you do not assing an ID, but ASP.NET does. The fact is that every control on the page has an ID. If you do not set one - ASp.NET generates one for you. ANyway, an idea - try setting ClientIDMode for these link buttons to `Static` and generate some ID for them yourself, and see if this resolves the issue. Just make sure of: 1) ID is unique for a signgle link button; 2) it is the same for a single button over postbacks; 3) You do this before adding button to the Controls collection – Andrei Apr 23 '14 at 12:54