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

Events in my dynamically loaded user controls do not fire in ASP.NET Web Application(我的动态加载的用户控件中的事件不会在 ASP.NET Web 应用程序中触发)
本文介绍了我的动态加载的用户控件中的事件不会在 ASP.NET Web 应用程序中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ASP.NET Web 应用程序,其中包含 MasterPage、ASPX 页面,我根据菜单选择将用户控件动态加载到包含 PlaceHolder 的 UpdatePanel 中.这工作正常.问题是我的用户控件中的任何事件都不起作用.我有不同的场景:

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,其中选定的值应加载不同的用户控件
  2. LinkBut​​ton,其中 Click 事件应将不同的 UserControl 加载到 PlaceHolder(在父页面上).

我现在已经花了 3 天的时间为此尝试了几件事.... 注册事件,使用接口等等....

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.

是的,我也在用户控件上尝试了 Page_PreInit 事件.当我在用户控件中单击 LinkBut​​ton(或 DropDownList)时唯一触发的是父页面上的 Page_Load 事件.然后包含用户控件(我正在单击)的 PlaceHolder 具有 Contol.Count = null这是一些代码

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;
    }

父页面上的 PlaceHolder 位于 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>

行为如下..当我加载我的 USerControl Page_Load 并运行 Click 事件时..当我点击我的用户控件中的链接按钮时,父页面上的 Page_Load 运行但我的用户控件中的 Click 方法不运行.用户控件(我刚刚单击了我的链接按钮)消失了.

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.

推荐答案

你需要在每次回发时重新创建动态控件,记住页面实例是根据请求创建的,如果你不重新创建控件那么它不会在 PostBack 上存在.

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每个页面访问的页面.添加这些控件的最佳时机是在页面生命周期的初始化阶段,即发生在加载视图状态阶段之前.也就是说,我们希望拥有控制层次结构在加载视图状态阶段到达之前完成.因此,最好为 Page 创建一个事件处理程序类的 Init 事件在您的代码隐藏类中,并添加您的动态控制在那里.

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.

这篇关于我的动态加载的用户控件中的事件不会在 ASP.NET Web 应用程序中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)