簡體   English   中英

如何使aspx頁面為只讀?

[英]How to make aspx pages Read Only?

我有一個條件,需要檢查是否migrated任何屬性,然后將相關的aspx頁面設置為read-only

如下所示:但是在這里我需要在所有必需的Pages上創建EnableControls() ,這將增加代碼行。

EnableControls(this.Page.Form.Controls, false); // will call this from Page_Load()

public void EnableControls(ControlCollection ctrl, bool isEnable)
{
    foreach (Control item in ctrl)
    {
        if (item.GetType() == typeof(Panel) || item.GetType() == typeof(HtmlGenericControl))
            EnableControls(item.Controls, isEnable);
        else 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;
    }
}

還有其他最簡單的方法來實現此目的,例如創建全局函數並從所需的read-only頁面中調用它嗎?

謝謝! 感謝任何更好的主意。

您可以像這樣在Page上使用Extension方法。

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)

 public partial class _Default : Page
    {
        public bool IsReadonly
        {
            get
            {
                return true;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

或在.aspx文件本身中,只要您像這樣使用它

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM