Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使aspx页面为只读?_C#_Asp.net_Webforms - Fatal编程技术网

C# 如何使aspx页面为只读?

C# 如何使aspx页面为只读?,c#,asp.net,webforms,C#,Asp.net,Webforms,我有一个条件,需要检查是否有任何属性已迁移,然后将相关的aspx页面设置为只读 如下所示:但这里我需要在所有需要的页面上创建启用控件(),这将增加代码行数 启用控件(this.Page.Form.Controls,false);//将从Page_Load()调用此函数。 有没有其他最简单的方法来实现这一点,比如创建一个全局函数并从所需的只读页面调用它 谢谢!非常感谢您的建议。您可以在这样的页面上使用扩展方法 public static class PageExtensions{ publ

我有一个条件,需要检查是否有任何属性已迁移,然后将相关的
aspx
页面设置为
只读

如下所示:但这里我需要在所有需要的页面上创建
启用控件()
,这将增加代码行数

启用控件(this.Page.Form.Controls,false);//将从Page_Load()调用此函数。

有没有其他最简单的方法来实现这一点,比如创建一个全局函数并从所需的
只读页面调用它


谢谢!非常感谢您的建议。

您可以在这样的页面上使用扩展方法

public static class PageExtensions{
    public static void EnableControls(this Page page,ControlCollection ctrl, bool isEnable)
            {
                if (ctrl == null)
                    ctrl = page.Controls;
                foreach (Control item in ctrl)
                {
                    if (item.Controls.Count > 0)
                        EnableControls(page, item.Controls, isEnable);

                    if (item.GetType() == typeof (DropDownList))
                        ((DropDownList) item).Enabled = isEnable;
                    else if (item.GetType() == typeof (TextBox))
                        ((TextBox) item).Enabled = isEnable;
                    else if (item.GetType() == typeof (Button))
                        ((Button) item).Enabled = isEnable;
                    else if (item.GetType() == typeof (HtmlInputButton))
                        ((HtmlInputButton) item).Disabled = !isEnable;
                }
            }
}

这将允许您只调用This.EnableControls(null,false)
来禁用页面上的所有控件

您可以在这样的页面上使用扩展方法

public static class PageExtensions{
    public static void EnableControls(this Page page,ControlCollection ctrl, bool isEnable)
            {
                if (ctrl == null)
                    ctrl = page.Controls;
                foreach (Control item in ctrl)
                {
                    if (item.Controls.Count > 0)
                        EnableControls(page, item.Controls, isEnable);

                    if (item.GetType() == typeof (DropDownList))
                        ((DropDownList) item).Enabled = isEnable;
                    else if (item.GetType() == typeof (TextBox))
                        ((TextBox) item).Enabled = isEnable;
                    else if (item.GetType() == typeof (Button))
                        ((Button) item).Enabled = isEnable;
                    else if (item.GetType() == typeof (HtmlInputButton))
                        ((HtmlInputButton) item).Disabled = !isEnable;
                }
            }
}

这将允许您只调用This.EnableControls(null,false)
禁用页面上的所有控件

您可以创建一个
IHttpModule
来截取该页面,通过反射检查该页面是否为只读页面,并基于此禁用其控件

 public class ReadOnlyModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += (_o, _e) =>
            {
                var handler = ((HttpApplication)_o).Context.CurrentHandler;

                var page = handler as Page;
                if (page != null)
                {
                    page.PreRender += (o, e) =>
                    {
                        var readonlyPropertyInfo = o.GetType().GetProperty("IsReadonly");
                        var shouldMakeItReadonly = readonlyPropertyInfo != null && (bool)readonlyPropertyInfo.GetValue(o) == true;
                        var isEnable = !shouldMakeItReadonly;
                        EnableControls(((Page)o).Controls, isEnable);
                    };

                }
            };
        }

        public void EnableControls(ControlCollection ctrl, bool isEnable)
        {
            foreach (Control item in ctrl)
            {
                if (item.HasControls())
                    EnableControls(item.Controls, isEnable);
                else if (item is WebControl)
                    ((WebControl)item).Enabled = isEnable;
                else if (item is HtmlControl)
                    ((HtmlControl)item).Disabled = !isEnable;
            }
        }

        public void Dispose()
        {

        }
    }
httpModules
部分注册此模块,然后可以在特定页面中实现IsReadonly属性

作为奖励,您还可以将此属性添加到标记(aspx)文件中,并依赖ASP.Net编译

用例

您可以将
IsReadonly
属性放在代码隐藏(default.aspx.cs)中

或者在.aspx文件本身中,只要您这样使用它

<script runat="server">
    public bool IsReadonly { get { return true; } }
</script>

公共bool为只读{get{return true;}}

您可以创建一个
IHttpModule
来截取页面,通过反射检查页面是否为只读页面,并基于此禁用其控件

 public class ReadOnlyModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += (_o, _e) =>
            {
                var handler = ((HttpApplication)_o).Context.CurrentHandler;

                var page = handler as Page;
                if (page != null)
                {
                    page.PreRender += (o, e) =>
                    {
                        var readonlyPropertyInfo = o.GetType().GetProperty("IsReadonly");
                        var shouldMakeItReadonly = readonlyPropertyInfo != null && (bool)readonlyPropertyInfo.GetValue(o) == true;
                        var isEnable = !shouldMakeItReadonly;
                        EnableControls(((Page)o).Controls, isEnable);
                    };

                }
            };
        }

        public void EnableControls(ControlCollection ctrl, bool isEnable)
        {
            foreach (Control item in ctrl)
            {
                if (item.HasControls())
                    EnableControls(item.Controls, isEnable);
                else if (item is WebControl)
                    ((WebControl)item).Enabled = isEnable;
                else if (item is HtmlControl)
                    ((HtmlControl)item).Disabled = !isEnable;
            }
        }

        public void Dispose()
        {

        }
    }
httpModules
部分注册此模块,然后可以在特定页面中实现IsReadonly属性

作为奖励,您还可以将此属性添加到标记(aspx)文件中,并依赖ASP.Net编译

用例

您可以将
IsReadonly
属性放在代码隐藏(default.aspx.cs)中

或者在.aspx文件本身中,只要您这样使用它

<script runat="server">
    public bool IsReadonly { get { return true; } }
</script>

公共bool为只读{get{return true;}}

完美。谢谢你,乔丹。!还是想问一下,除了检查
按钮
下拉列表
,还有没有其他方法可以禁用所有控件。。不使用if else?@RahulHendawe,您可以尝试只执行
item.Enabled=isEnable
。我不确定HtmlInputButton会发生什么,因为它使用Disabled。但是试试看:)完美。谢谢你,乔丹。!还是想问一下,除了检查
按钮
下拉列表
,还有没有其他方法可以禁用所有控件。。不使用if else?@RahulHendawe,您可以尝试只执行
item.Enabled=isEnable
。我不确定HtmlInputButton会发生什么,因为它使用Disabled。但是试试看:)谢谢看起来是个好主意。我以前没有听说过
IHttpModule
。您还可以分享一个链接或示例,说明如何在
httpModules
中注册此模块及其完整的工作原理吗?只要注册这个模块就行了。艾德里安:它就像一个符咒。我们可以从任何类文件调用
IsReadonly
此属性吗?意思是如果我们叫它怎么办?我们需要在当前函数中更改什么?是的,我将其标记为答案。谢谢看起来是个好主意。我以前没有听说过
IHttpModule
。您还可以分享一个链接或示例,说明如何在
httpModules
中注册此模块及其完整的工作原理吗?只要注册这个模块就行了。艾德里安:它就像一个符咒。我们可以从任何类文件调用
IsReadonly
此属性吗?意思是如果我们叫它怎么办?我们需要在当前函数中更改什么?是的,我将其标记为答案。