简体   繁体   中英

Nested control's events not firing in UserControl

Hello I have a weird issue. I have a user control which I load dinamycally using LoadControl. In this UserControl I have a button whith assigned OnClick event, but when I click that button, the event is not fired.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ServiceInput.ascx.cs"
Inherits="uPay.UserControls.ServiceInputs.ServiceInput" ViewStateMode="Disabled" %>
    <div class="PopupFoot">
        <asp:Button ID="btnAdd" CssClass="PopupADD" runat="server" OnClick="btnAdd_Click" />
    </div>

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //Method never gets called :/
    }

Here I load user control in the page

protected void ServicesUpdatePanel_Load(object sender, EventArgs e)
{
        string arg = Request.Params.Get("__EVENTARGUMENT");
        if (arg == "ServiceInput")
        {
            int serviceId;
            if (Int32.TryParse(hdnSelectedService.Value, out serviceId))
            {
                using (Entities db = new Entities())
                {
                    LocalizedServiceRecord service = db.ServiceRecords.OfType<LocalizedServiceRecord>().FirstOrDefault(s => s.Id == serviceId && s.Language.Id == CurrentLanguage.Id);
                    lblPopupTitle.Text = service.Name;
                    ServiceInputBase serviceInput = LoadInputControl(service);
                    InputServicePlaceHolder.Controls.Add(serviceInput);
                    ServicesUpdatePanel.Update();
                }
            }
        }
}

    private ServiceInputBase LoadInputControl(ServiceRecord service)
    {
        ServiceInputBase serviceInput = LoadControl("~/UserControls/ServiceInputs/ServiceInput.ascx") as ServiceInputBase;

        return serviceInput;
    }

Any Idea?

Long time now, but maybe someone will find it useful. The problem was that when I clicked btnAdd , it caused a postback, and before it was able to trigger btnAdd_Click , the user control was already destroyed.

The solution to this problem is to recreate the usercontrol on postback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM