设置页面所有控件只读。
2009-01-20 11:09
417 查看
/// <summary> /// 设置当前页面上的所有可输入和选择的控件为只读属性 /// </summary> /// <param name="page"></param> public static void SetControlReadOnly(Page page) { foreach (Control ctrl in page.Controls) { SetControlReadOnly(ctrl); } } //递归调用。设置控件只读。 public static void SetControlReadOnly(Control ctr) { if (ctr is TextBox) { TextBox txtControl = (TextBox)ctr; txtControl.ReadOnly = true; txtControl.Enabled = false; } else if (ctr is RadioButton) { RadioButton btn = (RadioButton)ctr; btn.Enabled = false; } else if (ctr is RadioButtonList) { RadioButtonList btn = (RadioButtonList)ctr; btn.Enabled = false; } else if (ctr is CheckBox) { CheckBox cb = (CheckBox)ctr; cb.Enabled = false; } else if (ctr is DropDownList) { DropDownList list = (DropDownList)ctr; list.Enabled = false; } else if (ctr is HtmlTextArea) { HtmlTextArea cb = (HtmlTextArea)ctr; cb.Attributes.Add("readonly", ""); cb.Disabled = true; } else if (ctr is HtmlSelect) { HtmlSelect rb = (HtmlSelect)ctr; rb.Disabled = true; } else if (ctr is HtmlInputCheckBox) { HtmlInputCheckBox rb = (HtmlInputCheckBox)ctr; rb.Disabled = true; } else if (ctr is HtmlInputRadioButton) { HtmlInputRadioButton rb = (HtmlInputRadioButton)ctr; rb.Disabled = true; } else if (ctr is HtmlInputText) { HtmlInputControl input = (HtmlInputControl)ctr; input.Attributes.Add("readonly", ""); input.Disabled = true; } else foreach (Control ctr1 in ctr.Controls) { SetControlReadOnly(ctr1); } }
因为是递归调用,把当前页面传入就可以了,方法会找页面中所有的控件。
/// <summary> /// 取消只读属性 /// </summary> /// <param name="page">要操作的页面,一般传this</param> /// <param name="ControlID">控件ID</param> public static void RemoveReadOnlyByID(Page page, string ControlID) { Control ctr = page.FindControl(ControlID); if (ctr is TextBox) { TextBox txtControl = (TextBox)ctr; txtControl.ReadOnly = false; txtControl.Enabled = true; } else if (ctr is RadioButton) { RadioButton btn = (RadioButton)ctr; btn.Enabled = true; } else if (ctr is CheckBox) { CheckBox cb = (CheckBox)ctr; cb.Enabled = true; } else if (ctr is DropDownList) { DropDownList list = (DropDownList)ctr; list.Enabled = true; } else if (ctr is HtmlTextArea) { HtmlTextArea cb = (HtmlTextArea)ctr; cb.Attributes.Remove("readonly"); cb.Disabled = false; } else if (ctr is HtmlSelect) { HtmlSelect rb = (HtmlSelect)ctr; rb.Disabled = false; } else if (ctr is HtmlInputCheckBox) { HtmlInputCheckBox rb = (HtmlInputCheckBox)ctr; rb.Disabled = false; } else if (ctr is HtmlInputRadioButton) { HtmlInputRadioButton rb = (HtmlInputRadioButton)ctr; rb.Disabled = false; } else if (ctr is HtmlInputText) { HtmlInputControl input = (HtmlInputControl)ctr; input.Attributes.Remove("readonly"); input.Disabled = false; } }
相关文章推荐
- 设置页面的所有控件为只读,不可用
- 遍历页面上所有的TextBox控件并将其设置为空值
- C# 设置本页面内所有TextBox为只读
- c# 遍历页面所有控件,设置panel隐藏
- asp.net 遍历aspx页面中的TextBox控件,并设置属性为只读
- 遍历页面所有控件并设为只读的方法
- 递归设置当前用户控件下的所有服务器控件为只读状态
- C# .NET 遍历一个Web页面中所有的TextBox控件 并设置读写属性
- 设置页面所有控件
- C#获取页面上的所有checkbox控件对象并设置属性
- 禁用asp.net页面中的所有控件 设置本页所有控件的Enabled = false的方法
- WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享
- android EditText 控件无法代码设置ReadOnly 只读
- Extjs 一次将formPanel里的所有项目属性设置成只读 .
- 遍历页面上所有一种类型的控件,并赋值
- [IE编程] WebBrowser控件中设置页面的缩放
- c#最大化设置窗体所有控件随之变化
- 递归输出ASP.NET页面所有控件的类型和ID的代码
- ASP.Net页面和控件缓存设置
- asp.net下遍历页面中所有的指定控件的代码