开发者

How can I enable/disable an asp.net form/controls

How can I enable/di开发者_C百科sable an asp.net form/controls selectively or entirely from code-behind?

The following code is not working. Coz there is no Enabled property in this case.

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. 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. The ASP.NET framework will not call the Render method of controls for which the Visible property is set to false.

From the documentation:

If this property is false, the server control is not rendered. You should take this into account when organizing the layout of your page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜