以编程方式将ASP.net用户控件添加到UpdatePanel [英] Programmatically adding ASP.net User Control to UpdatePanel

查看:68
本文介绍了以编程方式将ASP.net用户控件添加到UpdatePanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在这里真的很开心。我已经阅读了数以百计的论坛帖子,发现许多其他人已经复制了这个问题,但还没有找到解决方案。通过测试,我已经能够找到问题的原因,并将在文本中首先通过代码示例进行描述。


我想要做的目的是通过使用ASP.net AJAX UpdatePanels和用户控件创建一个无回复的Web应用程序。通过正常回发以编程方式将用户控件添加到网页时,一切正常。用户控件中的所有控件都在页面中正确注册,并且用户控件中包含的任何更新面板也可以正常工作。但是,如果不是使用完整的回发,而是使用UpdatePanel和UpdatePanel的部分页面更新,则控件不会在页面中注册,并且不会触发来自它们的事件(例如,按钮单击事件永远不会触发事件断点) )。


因为如果在完整的回发中加载或从命名空间动态添加的相同用户控件工作正常,我可以相对确定它只是在加载时遇到麻烦部分页面更新到UpdatePanel。我通过LoadConrol方法加载控件,然后通过PlaceHolder控件将其添加到页面。从理论上讲,将用户控件添加到PlaceHolder应该注册自己以及它与页面的控件和事件,但事实并非如此。


以下代码示例是UpdatePanel免费的使用可用的用户控件的页面,稍后我将使用没有的UpdatePanel显示相同的代码。


我想我需要弄清楚如何注册控件及其事件页面没有经过整页回发。任何建议??


此示例按预期工作:
Default.aspx:

Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have been able to find the cause of the problem, and will describe it here first textually and then through a code example.

The purpose of what I am trying to do is to create a postback-free web application through the use of ASP.net AJAX UpdatePanels and User Controls. When programmatically adding a User Control to a web page through a normal postback everything works fine. All the controls within the user control are registered properly in the page and any update panels included in the user control also work properly. HOWEVER, if instead of using a full postback you use an UpdatePanel and a partial page update of the UpdatePanel the controls do not get registered with the page and events from them do not fire (for instance, a button click event never hits the event breakpoint).

Because the very same user control works fine if loaded in a full postback or dynamically added from a namespace works fine, I can be relatively sure that it only is trouble when loading via a partial page update into an UpdatePanel. I load the control via the LoadConrol method and then add it to the page via a PlaceHolder control. Theoretically, adding the User Control to the PlaceHolder should register itself and it''s controls and events with the page, but it does not.

The following code sample is a UpdatePanel-free page using a user control that works, later I will show the same code with an UpdatePanel that does not.

I think I need to figure out how to register the controls and their events with the page without going through a full page postback. Any suggestions??

This example works as expected:
Default.aspx:

展开 | 选择 | Wrap | 行号

推荐答案

试试这个.... UpdatePanel.Controls.Add(control_name)而不是this.ContentTemplate.Controls.Add(control_name)。因为,这指的是整个页面,而不是指您的更新面板。


如果您的控件嵌入在Panel中,则Visibility问题很有效...因此,如果面板是不可见的,你的控件不会被加载。


问候,

Senaka
try this.... UpdatePanel.Controls.Add(control_name) instead of this.ContentTemplate.Controls.Add(control_name). Because, this refers to the page as a whole and not to your update panel.

the Visibility issue works well if your control is embedded inside a Panel... Therefore, if the panel is not visible, your controls wont get loaded.

Regards,
Senaka



尝试这个.... UpdatePanel.Controls.Add(control_name)而不是this.ContentTemplate.Controls.Add(control_name)。因为,这指的是整个页面,而不是指您的更新面板。


如果您的控件嵌入在Panel中,则Visibility问题很有效...因此,如果面板是不可见的,你的控件不会被加载。


问候,

Senaka
try this.... UpdatePanel.Controls.Add(control_name) instead of this.ContentTemplate.Controls.Add(control_name). Because, this refers to the page as a whole and not to your update panel.

the Visibility issue works well if your control is embedded inside a Panel... Therefore, if the panel is not visible, your controls wont get loaded.

Regards,
Senaka



嘿Senaka,

感谢您的建议。无论Controls.Add()使用哪种组合,只要将用户控件添加到UpdatePanel,它就不会产生影响。控件本身将加载,但其中没有任何控件或属性或事件可访问。


理想情况下,我希望能够继续使用UpdatePanels,PlaceHolders和User用于创建无回复UI的控件。现在我不得不在每个页面上只有UI AJAXified的部分回复。

Hey Senaka,
thanks for your suggestion. No matter which combination of Controls.Add() I use it doesn''t seem to make a difference as long as the User Control is added to a UpdatePanel. The control itself will load, but none of the controls or properties or events therein are accessible.

Ideally, I''d like to be able to keep using UpdatePanels, PlaceHolders, and User Controls to create a postback-free UI. Right now I''d have to settle for a postback on every page with only parts of the UI AJAXified.



Hey Senaka,

感谢您的建议。无论Controls.Add()使用哪种组合,只要将用户控件添加到UpdatePanel,它就不会产生影响。控件本身将加载,但其中没有任何控件或属性或事件可访问。


理想情况下,我希望能够继续使用UpdatePanels,PlaceHolders和User用于创建无回复UI的控件。现在,我必须在每个页面上只用部分UI AJAXified来解决回发问题。
Hey Senaka,
thanks for your suggestion. No matter which combination of Controls.Add() I use it doesn''t seem to make a difference as long as the User Control is added to a UpdatePanel. The control itself will load, but none of the controls or properties or events therein are accessible.

Ideally, I''d like to be able to keep using UpdatePanels, PlaceHolders, and User Controls to create a postback-free UI. Right now I''d have to settle for a postback on every page with only parts of the UI AJAXified.



我不知道这是否可以帮助你,但是你有没有想过将UpdatePanel放入用户控件而不是将用户控件添加到更新面板?


-Frinny

I don''t know if this can help you but have you thought about putting the UpdatePanel into your user control instead of adding your user control to the update panel?

-Frinny


这篇关于以编程方式将ASP.net用户控件添加到UpdatePanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆