-2

I am working on a project where I am creating a dynamic table.Now depending upon my condition I am adding link button to table cells,but the click event of my link button is not working.I am not getting why its not working,neither its showing any error. Following is my code

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 is 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();
                    //lnkevent.ClientIDMode ="Static";
                    lnkevent.ID = (i+j).ToString();
                    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.Click += lnkevent_OnClick;
                    lnkevent.Click += new EventHandler(lnkevent_OnClick);
                    ctCell.Controls.Add(lnkevent);
                }  
                tr.Cells.Add(ctCell);
            }
            tblcalendar.Rows.Add(tr);
        }
    }
}
 public void lnkevent_OnClick(object sender,EventArgs e)
{
    lblmonthname.Text = "hellooo";
    txttitle.Text = "";
}
9
  • I think the last link button must be working properly. Can u please check? Commented Apr 24, 2014 at 6:38
  • no its not working even Commented Apr 24, 2014 at 6:45
  • Have you tried using lnkevent.Click += new RoutedEventHandler(lnkevent_OnClick);? Commented Apr 24, 2014 at 6:52
  • No i haven't used what it will do if I will use it,I haven;t used this event handler before,can you please elaborate a little Commented Apr 24, 2014 at 6:53
  • It simply adds an event handler for your control. I forgot to mention that you'll need to replace EventArgs e with RoutedEventAgrs e in your method definition Commented Apr 24, 2014 at 6:59

1 Answer 1

2

Sounds like you are adding a button, but not binding an event listener to it. I don't know much about how asp.net does event binding, but it sounds like that could be your problem.

Maybe this link can help?

asp.net dynamically button with event handler

Sign up to request clarification or add additional context in comments.

2 Comments

I am not adding a button instead adding a link button please check my code and then suggest accordingly thanx
I have read the solution provided on the above link,but that will not work for me please suggest something else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.