how to disable controls of master page used in content page while processing some event in asp.net

心已入冬 提交于 2019-12-13 23:18:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!