简体   繁体   English

如何启用/禁用asp.net表单/控件

[英]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.

相关问题 如何禁用ASP.NET页面中的所有控件? - How do I disable all controls in ASP.NET page? 在asp.net中启用和禁用无法正常运行的控件 - Enable and disable of Controls not working properly in asp.net 如何使用Postmark进行ASP.NET登录控件? - How can I use Postmark for ASP.NET Login controls? 如何将控件异步添加到asp.net页? - How can I asynchronously add controls to a asp.net page? 如何在 asp.net 中启用 X Frame 选项 - How can i enable X Frame options in asp.net 在asp.net中,要启用/禁用图像按钮,如何在asp.net代码后面的文件中访问Gridview-&gt; Itemtemplate-&gt;面板的ImageButton控件 - In asp.net, To enable/disable imagebutton, How can I access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file 如何为ASP.NET中的某些控件禁用UpdateProgress? - How to disable UpdateProgress for certain controls in ASP.NET? 如何在选择usercontrol时禁用ASP.NET页面中的控件? - How to disable the controls in an ASP.NET page when the usercontrol is selected? 当我在asp.net中禁用viewstate并且我的viewstate太大时,我无法访问表单中的控件,我不想启用它 - can't access a control in my form when I disable viewstate in asp.net and my viewstate is too bulky I don't want to enable it 如何在ASP.NET中禁用下拉列表? - How can I disable a dropdownlist in ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM