EventHandler不适用于动态控制

时间:2013-01-15 13:27:15

标签: c# asp.net .net

我已提到Error with the event handlers of dynamic linkbutton。它表示要在Page_InitPage_Load中添加事件处理程序。我厌倦了代码。但是,当我单击动态添加的链接按钮时,不会触发事件处理程序。需要在这里纠正什么?

注意:在进行一些业务验证后,动态LinkBut​​ton控件会添加到按钮的单击事件中(为简洁起见,不会给出业务代码)

标记

<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>

代码背后

    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);
    }

3 个答案:

答案 0 :(得分:2)

必须在Page_Load / Page_Init中注册所有必需的动态控件的事件处理程序。在Dynamic Control’s Event Handler’s Working

可以看到一个工作示例

<强>标记

<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);
        }
    }

答案 1 :(得分:1)

必须在每次回发时重新创建动态控件,此Article是关于如何保持动态控件及其状态的良好链接。

答案 2 :(得分:-1)

将javascript onClick属性添加到dymanic控件并设置控件事件所需的隐藏字段值。 dymanic网格的Onclick,意志postback并将获得隐藏的字段值。在页面加载中调用一个方法来执行作业,如果隐藏字段有值,并在完成工作后使其null