在asp.net中处理某些事件时如何禁用内容页面中使用的母版页控件 [英] how to disable controls of master page used in content page while processing some event in asp.net

查看:98
本文介绍了在asp.net中处理某些事件时如何禁用内容页面中使用的母版页控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可以很好地禁用内容页面控件,但是如何禁用母版页面控件?

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;
            }
        }
    }
}

推荐答案

如果要禁用母版页上的所有控件,只需执行以下操作:

If you want to disable all the controls on a Master Page, just do the following:

DisableControls(this.Page.Master, isEnable);

或者,如果要对特定的MasterPage控制执行该方法:

Or, if you want to perform the method on a specific MasterPage contorl:

DisableControls(this.Page.Master.FindControl("Panel1"), isEnable);

更新:

为什么不将方法放在母版页上?

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);

这篇关于在asp.net中处理某些事件时如何禁用内容页面中使用的母版页控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆