问题
The following code works fine for disabling content page controls, but how do I disable master page controls?
public void DisableControls(Control control,bool isEnable)
{
if (control.HasControls())
{
foreach (Control c in control.Controls)
{
DisableControls(c, isEnable);
}
}
else
{
if (control is IPostBackDataHandler && !(control is IPostBackEventHandler))
{
if (control is WebControl)
{
((WebControl)control).Enabled = isEnable;
}
else if (control is HtmlControl)
{
((HtmlControl)control).Disabled = !isEnable;
}
}
}
}
回答1:
If you want to disable all the controls on a Master Page, just do the following:
DisableControls(this.Page.Master, isEnable);
Or, if you want to perform the method on a specific MasterPage contorl:
DisableControls(this.Page.Master.FindControl("Panel1"), isEnable);
Update:
Why don't you just put a method on your MasterPage:
public void SetControlEnabledState(bool enabled)
{
DisableControls(Menu1, enabled);
DisableControls(Control2, enabled);
}
Then, to access it, just do the following from any page that uses the master page:
((MasterPageName)this.Page.Master).SetControlEnabledState(enabled);
回答2:
The best way is to throw your custom event from your content page and then handle it in the master page. In this way make your master page independent from your content pages.
来源:https://stackoverflow.com/questions/3107609/how-to-disable-controls-of-master-page-used-in-content-page-while-processing-som