简体   繁体   English

嵌套控件的事件未在UserControl中触发

[英]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. 我有一个用户控件,可以使用LoadControl进行动态加载。 In this UserControl I have a button whith assigned OnClick event, but when I click that button, the event is not fired. 在此UserControl中,我有一个分配了OnClick事件的按钮,但是当我单击该按钮时,不会触发该事件。

<%@ 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. 问题是,当我单击btnAdd ,它引起了回发,并且在它能够触发btnAdd_Click ,用户控件已被销毁。

The solution to this problem is to recreate the usercontrol on postback. 解决此问题的方法是在回发时重新创建usercontrol。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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