views:

647

answers:

1

Hi,

So I need to add some link buttons to each cell of a asp:calendar control dynamically based on data in the database. I wondering what the best way to do this is so that the the link buttons will be wired up to their events on postbacks (as to my knowledge creating the link buttons on the Day Render event and adding them there will occur after the LinkButton events would have fired).

Does anyone have a nice way to handle this?

+1  A: 

EDIT: This is a hack but it works, you will have to get crafty with the event naming...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
}
rick schott
Doesn't the DayRender event come after the OnClick event? If so - then your answer won't work since the Click event has already been processed before the LinkButton gets initialized to handle it.
Streklin