如何在C#Web应用程序中将表单设为只读 [英] how to make a form readonly in c# web application

查看:93
本文介绍了如何在C#Web应用程序中将表单设为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使Web表单只读,因此我需要C#Web应用程序中的代码.

i want to make web form read only so i need the code in c# web application.

推荐答案

您可以使用以下代码来完成它,因为它更快,因为它有点递归.
You can do it with below code, it is faster because it is somewhat recursive.
EnableControls(this.Page.Form.Controls, false);





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


为什么在计算后不只是禁用Web窗体的所有控件?

快速浏览Google之后,我发现了以下代码段:

why don''t you just disable all controls of the webform after the calculation?

After a quick Google i found the following snippet:

public void enableAllControls(ControlCollection cc, bool enable)
{
    foreach (Control c in cc)
    {
        try { enableAllControls(c.Controls, enable); } catch { }
        if(c.GetType() == typeof(TextBox)){ try { ((TextBox)c).Enabled = enable; } catch { } }
        else if (c.GetType() == typeof(DropDownList)) { try { ((DropDownList)c).Enabled = enable; } catch { } }
        else if (c.GetType() == typeof(Button)) { try { ((Button)c).Enabled = enable; } catch { } }
        else if (c.GetType() == typeof(LinkButton)) { try { ((LinkButton)c).Enabled = enable; } catch { } }
        else if (c.GetType() == typeof(HtmlInputText)) { try {(HtmlInputText)c).Attributes.Add("disabled", "true"); } catch { } }
    }
}


This [^] should surely help you out.


这篇关于如何在C#Web应用程序中将表单设为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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