Add LinkButtons during ASP.NET Calendar Control OnDayRender event

So I need to add some link buttons to each cell of the asp: calendar control dynamically based on data in the database. I am wondering how best to do this so that the link buttons are connected to their back side events (as far as I know, the link buttons are created in the Day Render event and added after the LinkButton events are fired).

Does anyone have a good way to handle this?

+2


source to share


1 answer


EDIT: This is a hack, but it works, you will need to trick with naming events ... etc.

Markup:

<asp:Calendar id="calendar1" 
                OnDayRender="DayRender"
                runat="server">

<asp:LinkButton ID="LinkButton1" style="display:none;" runat="server" 
   onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

      



Code-Behind:

protected void DayRender(Object source, DayRenderEventArgs e) 
{

    LinkButton lb = new LinkButton();
    lb.ID = "LinkButton1";
    //set all your props
     lb.Attributes.Add("href", 
        "javascript:__doPostBack('" + Calendar1.UniqueID + "$" + lb.ClientID +"','')");

    e.Cell.Controls.Add(lb);

}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    //do something
}

      

+2


source







All Articles