0

我在获取以编程方式添加的用户控件中包含的按钮的单击事件以触发时遇到问题。我知道每次添加新的用户控件时都必须连接该事件,我很确定我正在这样做,但仍然没有。按钮的单击事件适用于第一个用户控件,它不是以编程方式添加的。这是用户控件的标记....

<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 后,所有后续的 AddressUserControls 都会添加到 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";
    }

编辑 - 好的,所以我将事件处理程序分配移动到 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++;
            }
        }

虽然不完全清楚为什么或如何解决这个问题。

4

1 回答 1

-1

当您以编程方式添加任何控件时,您需要在此之后注册单击事件。

于 2013-09-04T16:46:49.013 回答