简体   繁体   English

如何在aspx页面中添加或不添加控件?

[英]How to add or not a control in a aspx page?

I have a page with a web user control, this one is quite heavy and he is used only if the application user have some rights. 我有一个带有Web用户控件的页面,此页面很繁琐,只有在应用程序用户具有某些权限时才使用他。 So he is set to visible = false in case of user can't use it.. But in this case the control is added to page even he is not visible. 因此,如果用户无法使用它,则将其设置为visible = false 。但是在这种情况下,即使他不可见,该控件也会添加到页面中。

The problem is the web user control is used really Rarely. 问题是Web用户控件确实很少使用。 So I would like to not include code if this is not necessary. 因此,如果不需要,我不想包含代码。 I feel I have to use the preinit state to add the web user control when it is necessary but I don't know how.. Any good idea? 我觉得我必须在必要时使用preinit状态添加Web用户控件,但我不知道如何。 Thanks for help.. 感谢帮助..

add it in the code behind with LoadControl, eg. 将其添加到LoadControl后面的代码中,例如。 put PlaceHolder on that aspx page and then load it from the code, like this 将PlaceHolder放在该aspx页面上,然后从代码中加载它,就像这样

if (something)
{
  Control cnt = Page.LoadControl("UserControl.ascx");
  placeHolder1.Controls.Add(cnt);
}

What do you mean with quite heavy ? 笨重是什么意思? If it it time consuming because of its databinding, you should provide a public function (fe BindData ) that is not called automatically from page_load but from the page when it is requested and the user is authorized to see. 如果由于其数据绑定而浪费时间,则应提供一个public function (fe BindData ),该函数不会从page_load自动调用,而是在被请求并授权用户查看时从页面自动调用。

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

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