我在解决以编程方式添加的用户控件中包含的按钮的点击事件时遇到问题。我知道每次添加新的用户控件时都必须连接事件,我很确定我这样做,但仍然没有。该按钮的单击事件适用于第一个用户控件,即不以编程方式添加。这是用户控件的标记....
<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>
...这里是主页的标记......
<form id="form1" runat="server">
<div>
<My:FormUserControl runat="server" ID="myFormUserControl" />
<br /><br />
<hr />
<My:AddressUserControl runat="server" ID="myAddressUserControl" />
<br /><br />
<asp:PlaceHolder ID="phAddresses" runat="server"></asp:PlaceHolder>
<br /><br />
<asp:Button ID="btnAddAddress" runat="server" Text="Add Another Address" OnClick="btnAddAddress_Click" />
</div>
<br /><br />
<hr />
<asp:Button ID="btnSubmit" runat="server" Text="Create PDF" OnClick="btnSubmit_Click" />
</form>
..如您所见,它已经以声明方式包含一个AddressUserControl。单击btnAddAddress后,所有后续的AddressUserControl都将添加到phAddresses。 AddressUserControls以这种方式添加到后面的代码中......
private static List<AddressUserControl> addresses = new List<AddressUserControl>();
protected void Page_PreInit(object sender, EventArgs e)
{
int addressCount = 0;
foreach (AddressUserControl aCntrl in addresses)
{
Literal ltlSpace = new Literal();
ltlSpace.Text = "<br /><br />";
phAddresses.Controls.Add(aCntrl);
phAddresses.Controls.Add(ltlSpace);
addressCount++;
}
}
When btnAddAddress is click this event handler runs...
protected void btnAddAddress_Click(object sender, EventArgs e)
{
AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
findAddressControlRemoveButton(aCntrl);
addressUserControlButton.ID = "btnRemoveAddress" + addresses.Count + 1;
addressUserControlButton.Click += new EventHandler(addressUserControlButton_Click);
addresses.Add(aCntrl);
}
...这里是addressUserControlButton事件处理程序。这个从不运行,我想我不是在上面的处理程序中正确添加它?
private void addressUserControlButton_Click(object sender, EventArgs e)
{
Button thisButton = sender as Button;
thisButton.Text = "Why Hello";
}
编辑 - 好的,所以我将eventhandler赋值移到了Page_PreInit而不是btnAddAddress_Click,就像这样....
protected void Page_PreInit(object sender, EventArgs e)
{
int addressCount = 0;
foreach (AddressUserControl aCntrl in addresses)
{
Literal ltlSpace = new Literal();
ltlSpace.Text = "<br /><br />";
phAddresses.Controls.Add(aCntrl);
findAddressControlRemoveButton(aCntrl);
addressUserControlButton.ID = "btnRemoveAddress" + addressCount;
addressUserControlButton.Click += new EventHandler(addressUserControlButton_Click);
phAddresses.Controls.Add(ltlSpace);
addressCount++;
}
}
不清楚为什么或如何解决这个问题。
答案 0 :(得分:-1)
以编程方式添加任何控件时,您需要在此之后注册click事件。