Add LinkButtons during OnDayRender event of ASP.NE

2019-02-11 03:38发布

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条回答
疯言疯语
2楼-- · 2019-02-11 04:27

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
}
查看更多
登录 后发表回答