在我的动态加载的用户控件的事件不会在ASP.NET Web应用程序火我的、会在、控件、应用程序

由网友(像颩①樣の孩紙)分享简介:我有一个包含母版,ASPX页面,在这里我动态加载的用户控件到基于菜单选择含占位符的UpdatePanel ASP.NET Web应用程序。这工作得很好。现在的问题是,在我的用户控件没有事件工作..我有不同的方案:I have ASP.NET Web Application that contains MasterPa...

我有一个包含母版,ASPX页面,在这里我动态加载的用户控件到基于菜单选择含占位符的UpdatePanel ASP.NET Web应用程序。这工作得很好。现在的问题是,在我的用户控件没有事件工作..我有不同的方案:

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:

的DropDownList,在选择时值应加载不同的用户控件 的LinkBut​​ton,在Click事件应加载不同的用户控件到占位符(父页)。

我已经花了3天这个现在,并试图为这个几件事情....登记的活动,除其他事项外使用的接口....

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

但迄今没有奏效:( ..thankful任何帮助,我可以得到的。

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

是的,我曾尝试在用户控制PAGE_ preINIT事件也是如此。 唯一的一点,当我点击我的用户控制我的LinkBut​​ton(或DropDownList的)火灾是在父page..and Page_Load事件则包含用户控件(我点击)占位符有Contol.Count = NULL 下面是一些code

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在我的用户

//Page_Load in my Usercontrol

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

父页面上的占位符是内部的UpdatePanel >>的ContentTemplate。

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>

该行为是following..when我打开我的用户的Page_Load的Click事件runs..when我点击我的LinkBut​​ton在我的用户,Page_Load中的父页面运行,但不是我的Click方法在我的用户。用户控件(我刚刚点击我的LinkBut​​ton上)消失。

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.

推荐答案

您需要在每一个回传重新创建动态控制时,请记住页面实例被每个请求创建的,如果不重新创建控制则在回发不会存在。

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.

看这里

提取

动态添加控件必须编程方式添加到Web   在每一页和每一个页面的访问。最好及时补充这些控件   是在页面生命周期,初始化阶段,其中   时的负载视图状态阶段之前。也就是说,我们希望有   控制层次完整加载视图状态的阶段到来之前。   出于这个原因,最好是创建一个事件处理程序页   类的初始化事件在code-behind类,并添加动态   控制那里。

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.

阅读全文

相关推荐

最新文章