动态控制的事件处理程序的工作

时间:2013-01-16 17:36:24

标签: c# asp.net .net

我有以下代码,可以在普通按钮的click事件上添加动态LinkBut​​ton。 LinkBut​​tons需要一个EventHandler “LinkButton_Click“。由于事件处理程序需要在Page_Load/Page_Init本身注册时,我首先在Page_Load中添加所有可能的链接(在回发场景中)。 (总共有四个链接按钮;根据业务场景,屏幕上只显示两个)

代码工作正常,并且在click事件上调用事件处理程序。

注意:业务场景是 - 如果当前时间的第二部分小于30,则需要显示前两个LinkBut​​tons;如果大于30,则需要显示最后两个LinkBut​​tons。

注意:清除控件并在PopulateLinksBasedOnCriteria()方法

中再次添加

问题

  1. PopulateLinksBasedOnCriteria()方法中,我新建了链接按钮实例。事件处理程序仍在触发。这是有效的,因为在Page_Load中添加的ID是相同的。是否有任何MSDN参考证实了这种行为? (即,事件处理程序将基于Page_Load/Page_Init时注册的ID。即使清除并再次添加控件,这也将起作用)
  2. 创建LinkBut​​tons时有一些重复的代码 - 一个在Page_Load内,另一个在PopulateLinksBasedOnCriteria()内。有没有更好的方法来实现这个业务任务而没有重复的代码?
  3. 标记

    <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 个答案:

答案 0 :(得分:2)

我建议在Page_Load事件On postback, how can I check which control cause postback in Page_Init event中检测导致PostBack的原因然后

 if (Page.IsPostBack && *PostBackControl.Name=="btnAdd"*)
            {
if (DateTime.Now.Second < 30)
        {
                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);
    } else
        {
                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);
    }
            }
        }