[英]How can I enable/disable an asp.net form/controls
How can I enable/disable an asp.net form/controls selectively or entirely from code-behind? 如何从代码隐藏中有选择地或完全启用/禁用asp.net表单/控件?
The following code is not working. 以下代码不起作用。 Coz there is no Enabled property in this case.
因为在这种情况下,没有Enabled属性。
public static void Disable(Page container)
{
for (int i = 0; i < container.Controls.Count; i++)
{
container.Form.Controls[i].Enabled = false;
}
}
Only the controls that inherit from WebControl will have a Enabled
property. 仅继承自WebControl的控件将具有
Enabled
属性。 So you could do something like this inside your loop: 因此,您可以在循环中执行以下操作:
var webControl = container.Form.Controls[i] as WebControl;
if(webControl != null) {
webControl.Enabled=false;
}
You may use Visible
instead of Enabled
. 您可以使用
Visible
代替Enabled
。 The ASP.NET framework will not call the Render method of controls for which the Visible property is set to false. ASP.NET框架将不会调用Visible属性设置为false的控件的Render方法。
From the documentation: 从文档中:
If this property is false, the server control is not rendered.
如果此属性为false,则不呈现服务器控件。 You should take this into account when organizing the layout of your page.
在组织页面布局时,应考虑到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.