在ASP.NET中添加动态控件

时间:2013-09-06 20:35:26

标签: c# asp.net dynamic user-controls webforms

在我看来,在ASP.NET Web窗体中动态添加控件存在固有的困难。具体来说,要将动态添加的控件包含在ViewState中,并将其事件正确连接等等,建议在Page_PreInit事件期间添加这些控件。

尽管如此,很多时候我们可能希望根据事件添加此类控件,例如用户单击按钮。控制特定事件(如Click事件)始终在Init之后运行,甚至在Load之后运行。假设以下.aspx ....

 <form id="form1" runat="server">
    <div>
      <asp:PlaceHolder ID="phAddresses" runat="server"></asp:PlaceHolder>
       <br /><br />
        <asp:Button ID="btnAddAddress" runat="server" Text="Add Another Address" OnClick="btnAddAddress_Click" />

...以及以下.aspx.cs ....

private static List<AddressUserControl> addresses = new List<AddressUserControl>();




protected void Page_PreInit(object sender, EventArgs e)
  {

   foreach (AddressUserControl aCntrl in addresses)
    {
      phAddresses.Controls.Add(aCntrl);
       // Helper to find button within user control
     addressButtonControl = findAddressControlRemoveButton(aCntrl);
     addressUserControlButton.ID = "btnRemoveAddress" + addressCount;
     addressUserControlButton.Click += new EventHandler(addressUserControlButton_Click);
     addressCount++;
    }
  }

 protected void btnAddAddress_Click(object sender, EventArgs e)
 {
  AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx")                 as    AddressUserControl;
 addresses.Add(aCntrl);
 }

现在,在上述情况下,显示的用户控件数总是落后于用户实际添加的数字,因为Click事件直到PreInit之后才会运行,其中必须将控件添加到占位符。我必须在这里遗漏一些东西,因为这似乎是ASP生命周期固有的,并且需要一些“黑客”或其他。

编辑 - 是的由于某种原因我在btnAddress_Click()期间添加的EventHandlers将不会运行,只有我在Page_Init期间添加的EventHandlers,或者在标记中以声明方式添加。这就是我试图做的......

protected void btnAddAddress_Click(object sender, EventArgs e)
 {
   AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
  addresses.Add(aCntrl);
  phAddresses.Controls.Add(aCntrl);
  findAddressControlRemoveButton(aCntrl);
  addressUserControlButton.ID = "btnRemoveAddress" + addresses.Count;

                ///////////////////////////////////////////////////////////////////////////////////
                // ADDED EVENT HANDLER HERE
                //////////////////////////////////////////////////////////////////////////////////////
  addressUserControlButton.Click += new E ventHandler(addressUserControlButton_Click);

  }

......但事件不会像我说的那样开火。有什么想法吗?

编辑 - 这是我的AddressUserControl的标记。文件背后的代码中没有逻辑

<%@ Control Language="C#" ClassName="AddressUserControl" AutoEventWireup="true" CodeBehind="AddressUserControl.ascx.cs" Inherits="XFAWithUserControl.UserControls.AddressUserControl" %>


    <asp:Panel ID="pnlAddressForm" runat="server">
        <asp:Label ID="lblStreet" runat="server" Text="Street Address"></asp:Label>
           <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox>
             <br /><br />
            <asp:Label ID="lblCity" runat="server" Text="City"></asp:Label>
            <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblState" runat="server" Text="State"></asp:Label>
            <asp:TextBox ID="txtState" runat="server"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblZip" runat="server" Text="Zip"></asp:Label>
            <asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
            <br /><br />
            <asp:Button ID="btnRemoveAddress" runat="server" Text="Remove Address" />
        </asp:Panel>

现在我对btnRemoveAddress的点击事件就是这样的傻事......

 private void addressUserControlButton_Click(object sender, EventArgs e)
  {
    Button thisButton = sender as Button;
    thisButton.Text = "Why Hello";
  }

但我的目标是让它删除关联的AddressUserControl,以便用户可以通过单击按钮从页面添加和/或删除任意数量的AddressUserControl。

编辑 - 这就是我现在所拥有的,仍然无效

protected void btnAddAddress_Click(object sender, EventArgs e)
        {
            AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
            addresses.Add(aCntrl);
            phAddresses.Controls.Add(aCntrl);
            findAddressControlRemoveButton(aCntrl);
            addressUserControlButton.ID = "btnRemoveAddress" + addresses.Count;
            aCntrl.ChangeText += new EventHandler(addressUserControlButton_Click);
        }

private void addressUserControlButton_Click(object sender, EventArgs e)
{
    Button thisButton = sender as Button;
    thisButton.Text = "Why Hello";
}

AddressUserControl.ascx

<asp:Panel ID="pnlAddressForm" runat="server">
    <asp:Label ID="lblStreet" runat="server" Text="Street Address"></asp:Label>
    <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Label ID="lblCity" runat="server" Text="City"></asp:Label>
    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Label ID="lblState" runat="server" Text="State"></asp:Label>
    <asp:TextBox ID="txtState" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Label ID="lblZip" runat="server" Text="Zip"></asp:Label>
    <asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Button ID="btnRemoveAddress" runat="server" Text="Remove Address" OnClick="btnRemoveAddress_Click" />
</asp:Panel>

AddressUserControl.ascx.cs

public event EventHandler ChangeText;



   protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void btnRemoveAddress_Click(object sender, EventArgs e)
    {
        if (this.ChangeText != null)
        {
            ChangeText(sender, e);
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要让点击处理程序中的逻辑添加到面板的Controls集合以及控件列表中,如下所示:

protected void btnAddAddress_Click(object sender, EventArgs e)
{
    AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
    addresses.Add(aCntrl);
    phAddresses.Controls.Add(aCntrl);
}

更新:

在您的用户控件中,您需要定义一个可以在托管用户控件的页面中定义的事件,如下所示:

AddressUserControl.cs(代码隐藏):

public event EventHandler RemoveAddress;

protected void removeAddressUserControlButton_Click(object sender, EventArgs e)
{
    // Find out if the event has been set, if so then call it
    if (this.RemoveAddress!= null)
    {
        RemoveAddress(sender, e);
    }
}

现在,在您使用用户控件的页面上,执行以下操作:

// Wire up the user control's event
nameOfUserControl.RemoveAddress += new EventHandler(addressUserControlButton_Click);

最后,实施addressUserControlButton_Click事件:

protected void addressUserControlButton_Click(object sender, EventArgs e)
{
    // Do your dynamic control creation here or whatever else you want on the page
}