简体   繁体   English

如何在用户控件中获取javascript以在页面加载可见性为false的面板中工作?

[英]How go I get the javascript in my user control to work inside of a panel whose visibility is false on page load?

I am working on an ASP.NET/C# project in which we are placing a user control into an AJAX panel whose visibility is set to false on page load. 我正在开发一个ASP.NET/C#项目,我们在其中将用户控件放入AJAX面板中,该面板的可见性在页面加载时设置为false。 The visibility of this panel is set to true once the user submits some parameters. 用户提交一些参数后,此面板的可见性将设置为true。 However, although the user control and all of the other panel contents are visible once these parameters are set and submitted, the uc's javascript functions do not work. 但是,尽管一旦设置并提交了这些参数,即可看到用户控件和所有其他面板内容,但uc的javascript函数不起作用。

I attempted to solve this issue by adding Page.Controls.Add(controlID) in Page_Load if it is not a postback. 我试图通过在Page_Load中添加Page.Controls.Add(controlID)(如果它不是回发)来解决此问题。 The functionality is all there when I do this, but I know that there is a better way to get the functionality because in this case the control is being added to the page twice. 当我执行此操作时,功能就已存在,但是我知道有一种更好的方法来获得功能,因为在这种情况下,控件被两次添加到页面中。

EDIT: I put the controller in a div outside of the panel and got the control that I wanted out of it. 编辑:我将控制器放在面板外部的div中,并得到我想要的控件。 However, if anyone can explain why the javascript of the control wasn't defined or offer a solution that keeps my UC in the panel, I would really appreciate it. 但是,如果任何人都可以解释为什么未定义控件的javascript或提供将我的UC保留在面板中的解决方案,我将不胜感激。

If for asp server control the visible property is set to false, then that control is not even rendered on the page. 如果将ASP服务器控件的visible属性设置为false,则该控件甚至不会在页面上呈现。 Since the User Control is inside the ajax server control, the user control is not rendered. 由于用户控件位于ajax服务器控件内部,因此不会呈现该用户控件。 Javascript code is executed by the browser hence it must be present on the page to run. Javascript代码是由浏览器执行的,因此必须在页面上显示才能运行。 If you can set css property display: none to hide and display: block to show for the ajax server control, then user control and its javascript will be rendered on the page, however hidden from user for css property i set to display: none, so its javascript will be executed. 如果您可以为ajax服务器控件设置css属性显示:无,则将其隐藏并显示:块,则用户控件及其javascript将在页面上呈现,但是对用户隐藏的css属性我设置为显示:无,因此将执行其javascript。

To set display property of ajax server control, you could use: ajaxControl.Styles.Add("display", "none"). 要设置ajax服务器控件的显示属性,可以使用:ajaxControl.Styles.Add(“ display”,“ none”)。 Hope this helps you. 希望这对您有所帮助。

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

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