Events in my dynamically loaded user controls do n

2020-04-29 14:39发布

问题:

I have ASP.NET Web Application that contains MasterPage, ASPX Page where I load User Controls dynamically into UpdatePanel containing PlaceHolder based on menu selection. This works fine. The problem is that no events in my User Controls work.. I have different scenarios:

  1. DropDownList, where selected value should load different UserControls
  2. LinkButton, where Click event should load different UserControl into the PlaceHolder (on the parent page).

I have spent 3 days on this now and tried several things for this.... Registered events, used Interface among other things....

but so far nothing has worked :( ..thankful for any help I can get.

Yes, i have tried Page_PreInit event on the User Control as well. The only thing that fires when I click my LinkButton (or DropDownList) in my user control is the Page_Load event on the parent page..and then the PlaceHolder containing the user control (i am clicking) has Contol.Count = null Here is some code

//Page_Load on parent page:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadDefaultControl(ctrlPlaceHolder, Base_Path + "_Services.ascx");
        }
    }

//Page_Load in my Usercontrol

protected void Page_Load(object sender, EventArgs e)
    {
        btJoomlaManagement.Click += btJoomlaManagement_Click;
    }

The PlaceHolder on the parent page is inside a UpdatePanel >> ContentTemplate.

               <asp:UpdatePanel ID="pnlControlContainer" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:PlaceHolder ID="ctrlPlaceHolder" runat="server" EnableViewState="False" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="menuServices" />
                </Triggers>
            </asp:UpdatePanel>

The behavior is following..when I load my USerControl Page_Load with the Click event runs..when I click my Linkbutton in my usercontrol, Page_Load on the parent page runs but not my Click method in my usercontrol. The usercontrol (that I just clicked my Linkbutton on) disappears.

So my event (click) does not fire... hope you understand a little better.

回答1:

You need to re-create dynamic control on every single postback, remember the Page instance is created per request, if you do not re-create the control then it wont exist on PostBack.

Of course your control disappears, you didnt re-create it on the postback.

see here

Extract:

Dynamically added controls must be programmatically added to the Web page on each and every page visit. The best time to add these controls is during the initialization stage of the page life cycle, which occurs before the load view state stage. That is, we want to have the control hierarchy complete before the load view state stage arrives. For this reason, it is best to create an event handler for the Page class's Init event in your code-behind class, and add your dynamic controls there.



回答2:

Have you tried attaching the event in the Page_Init event instead of the Page_Load? This is how I do event assignment in my dynamic-control applications, haven't had any problems with that. Also, you mention this is being done in an UpdatePanel, is it in the content template? If so, are you adding appropriate triggers to it (If indeed you want this to happen in an AJAX manner)?

Are you setting the "autopostback" property to true?

Seeing some code would definitely help...



回答3:

It seems to me that the problem is the order of attaching events.

Page_load in USER controls in assynchronous cases like what we have present here is not the right spot to attach handlers. Is to late in the event pipeline.

You should try make your handler attach in the page_init. I have tried with a litle example and works like a charm.

Let us know if this had solve your problem