views:

48

answers:

2

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  A: 

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);
GenericTypeTea
but this is not working fine please suggest another solution
ush
@ush - Just saying 'not working fine' is not helpful. What's not working about it? What's the error? What code have you tried?
GenericTypeTea
I have to disable menu control in master pageso i have used //DisableControls(this.Page.Master.FindControl ("Menu1") ,false );//
ush
Did you try if `this.Page.Master.FindControl ("Menu1")` returns correctly your control?
Teddy
I have tried its not returning it
ush
Well, the most often problem is that "Menu1" is not the correct id. Check the `Menu1.ClientID` in the master page. Also the ASP.Net 4.0 allows you to manage the ClientID. Other option for your problem is that the menu is not contained in the Master page directly, but in another control that child of MasterPage.
Teddy
@ush - I've updated my answer.
GenericTypeTea
Thank You so much for allthe following code is working fine for me Menu mpLabel = (Menu)Master.FindControl("Menu1"); ((WebControl)mpLabel).Enabled = false;
ush
@usb - Don't forget to accept the answer that helped you the most. You need to get your accept rate up!
GenericTypeTea
+1  A: 

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.

Teddy