1

I have referred Error with the event handlers of dynamic linkbutton . It says to add event handlers in Page_Init or Page_Load. I tired following code. But the event handler is not fired when I click on the dynamic added link buttons. What need to be corrected here?

Note: The dynamic LinkButton controls are added in the click event of a button after some business validations (the business code is not given for brevity)

Markup

<form id="form1" runat="server">
<div>
    <asp:LinkButton ID="lnkTest" runat="server" OnClick="LinkButton_Click">Static LinkButton</asp:LinkButton>
    <br />
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    <br />
    <asp:PlaceHolder ID="plhDynamicLinks" runat="server"></asp:PlaceHolder>
</div>
</form>

Code Behind

    protected void Page_Load(object sender, EventArgs e)
    {

        foreach (Control ctrl in plhDynamicLinks.Controls)
        {
            LinkButton dynamicButton = (LinkButton)ctrl;
            dynamicButton.Click += new EventHandler(LinkButton_Click);
        }

        if (Page.IsPostBack)
        {

        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        int x = 0;

        foreach (Control ctrl in plhDynamicLinks.Controls)
        {
            LinkButton dynamicButton = (LinkButton)ctrl;
            dynamicButton.Click += new EventHandler(LinkButton_Click);
        }
    }

    protected void LinkButton_Click(object sender, EventArgs e)
    {
        LinkButton clickedControl = (LinkButton)sender;
        Response.Write(clickedControl.ID +" Link Button Clicked");
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        plhDynamicLinks.Controls.Clear();

        LinkButton button1 = new LinkButton();
        button1.ID = "D1";
        button1.Text = "1";
        plhDynamicLinks.Controls.Add(button1);

        LinkButton button2 = new LinkButton();
        button2.ID = "D2";
        button2.Text = "2";
        plhDynamicLinks.Controls.Add(button2);
    }
4

3 回答 3

2

必须在 Page_Load/ Page_Init 本身中注册所有必需的动态控件的事件处理程序。在动态控制的事件处理程序的工作中可以看到一个工作示例

标记

<form id="form1" runat="server">
<div>
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    <br />
    <asp:PlaceHolder ID="plhDynamicLinks" runat="server"></asp:PlaceHolder>
</div>
</form>

代码背后

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Page.IsPostBack)
        {
            LinkButton lnk1 = new LinkButton();
            lnk1.ID = "D1";
            lnk1.Text = "A";
            //Event handler must be registered in the Page_Load/Page_Init
            lnk1.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk1);

            LinkButton lnk2 = new LinkButton();
            lnk2.ID = "D2";
            lnk2.Text = "B";
            lnk2.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk2);

            LinkButton lnk3 = new LinkButton();
            lnk3.ID = "D3";
            lnk3.Text = "C";
            lnk3.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk3);

            LinkButton lnk4 = new LinkButton();
            lnk4.ID = "D4";
            lnk4.Text = "D";
            lnk4.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk4);

        }
    }

    protected void LinkButton_Click(object sender, EventArgs e)
    {
        PopulateLinksBasedOnCriteria();
        LinkButton clickedControl = (LinkButton)sender;
        Response.Write(DateTime.Now.ToString()+"___"+ clickedControl.ID + " Link Button Clicked" );
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        PopulateLinksBasedOnCriteria();
    }

    private void PopulateLinksBasedOnCriteria()
    {
        plhDynamicLinks.Controls.Clear();

        if (DateTime.Now.Second < 30)
        {
            LinkButton linkButton1 = new LinkButton();
            linkButton1.ID = "D1";
            linkButton1.Text = "1";
            plhDynamicLinks.Controls.Add(linkButton1);

            LinkButton linkButton2 = new LinkButton();
            linkButton2.ID = "D2";
            linkButton2.Text = "2";
            plhDynamicLinks.Controls.Add(linkButton2);
        }
        else
        {
            LinkButton linkButton3 = new LinkButton();
            linkButton3.ID = "D3";
            linkButton3.Text = "3";
            plhDynamicLinks.Controls.Add(linkButton3);

            LinkButton linkButton4 = new LinkButton();
            linkButton4.ID = "D4";
            linkButton4.Text = "4";
            plhDynamicLinks.Controls.Add(linkButton4);
        }
    }
于 2013-01-21T12:39:33.077 回答
1

必须在每次回发时重新创建动态控件,这篇文章是关于如何持久化动态控件及其状态的一个很好的链接。

于 2013-01-21T12:48:20.707 回答
-1

将 javascriptonClick属性添加到动态控件并设置控件事件所需的隐藏字段值。Onclick动态网格的意志postback和意志得到隐藏场值。如果隐藏字段有值,则在页面加载中调用一个方法来完成工作,并在null完成工作后完成。

于 2016-01-12T12:57:41.303 回答