/// <summary><br /> /// 設定當前頁面上的所有可輸入和選擇的控制項為唯讀屬性<br /> /// </summary><br /> /// <param name="page"></param><br /> public static void SetControlReadOnly(Page page)<br /> {</p><p> foreach (Control ctrl in page.Controls)<br /> {<br /> SetControlReadOnly(ctrl);</p><p> }<br /> }</p><p> //遞迴調用。設定控制項唯讀。<br /> public static void SetControlReadOnly(Control ctr)<br /> {<br /> if (ctr is TextBox)<br /> {<br /> TextBox txtControl = (TextBox)ctr;<br /> txtControl.ReadOnly = true;<br /> txtControl.Enabled = false;</p><p> }<br /> else if (ctr is RadioButton)<br /> {<br /> RadioButton btn = (RadioButton)ctr;<br /> btn.Enabled = false;</p><p> }<br /> else if (ctr is RadioButtonList)<br /> {<br /> RadioButtonList btn = (RadioButtonList)ctr;<br /> btn.Enabled = false;<br /> }</p><p> else if (ctr is CheckBox)<br /> {<br /> CheckBox cb = (CheckBox)ctr;<br /> cb.Enabled = false;<br /> }<br /> else if (ctr is DropDownList)<br /> {<br /> DropDownList list = (DropDownList)ctr;<br /> list.Enabled = false;<br /> }</p><p> else if (ctr is HtmlTextArea)<br /> {<br /> HtmlTextArea cb = (HtmlTextArea)ctr;<br /> cb.Attributes.Add("readonly", "");<br /> cb.Disabled = true;<br /> }<br /> else if (ctr is HtmlSelect)<br /> {<br /> HtmlSelect rb = (HtmlSelect)ctr;<br /> rb.Disabled = true;<br /> }</p><p> else if (ctr is HtmlInputCheckBox)<br /> {<br /> HtmlInputCheckBox rb = (HtmlInputCheckBox)ctr;<br /> rb.Disabled = true;<br /> }<br /> else if (ctr is HtmlInputRadioButton)<br /> {<br /> HtmlInputRadioButton rb = (HtmlInputRadioButton)ctr;<br /> rb.Disabled = true;<br /> }<br /> else if (ctr is HtmlInputText)<br /> {<br /> HtmlInputControl input = (HtmlInputControl)ctr;<br /> input.Attributes.Add("readonly", "");<br /> input.Disabled = true;<br /> }<br /> else<br /> foreach (Control ctr1 in ctr.Controls)<br /> {<br /> SetControlReadOnly(ctr1);<br /> }<br /> }
因為是遞迴調用,把當前頁面傳入就可以了,方法會找頁面中所有的控制項。
/// <summary><br /> /// 取消唯讀屬性<br /> /// </summary><br /> /// <param name="page">要操作的頁面,一般傳this</param><br /> /// <param name="ControlID">控制項ID</param><br /> public static void RemoveReadOnlyByID(Page page, string ControlID)<br /> {</p><p> Control ctr = page.FindControl(ControlID);<br /> if (ctr is TextBox)<br /> {<br /> TextBox txtControl = (TextBox)ctr;<br /> txtControl.ReadOnly = false;<br /> txtControl.Enabled = true;</p><p> }<br /> else if (ctr is RadioButton)<br /> {<br /> RadioButton btn = (RadioButton)ctr;<br /> btn.Enabled = true;</p><p> }</p><p> else if (ctr is CheckBox)<br /> {<br /> CheckBox cb = (CheckBox)ctr;<br /> cb.Enabled = true;<br /> }<br /> else if (ctr is DropDownList)<br /> {<br /> DropDownList list = (DropDownList)ctr;<br /> list.Enabled = true;<br /> }</p><p> else if (ctr is HtmlTextArea)<br /> {<br /> HtmlTextArea cb = (HtmlTextArea)ctr;<br /> cb.Attributes.Remove("readonly");<br /> cb.Disabled = false;<br /> }<br /> else if (ctr is HtmlSelect)<br /> {<br /> HtmlSelect rb = (HtmlSelect)ctr;<br /> rb.Disabled = false;</p><p> }</p><p> else if (ctr is HtmlInputCheckBox)<br /> {<br /> HtmlInputCheckBox rb = (HtmlInputCheckBox)ctr;<br /> rb.Disabled = false;<br /> }<br /> else if (ctr is HtmlInputRadioButton)<br /> {<br /> HtmlInputRadioButton rb = (HtmlInputRadioButton)ctr;<br /> rb.Disabled = false;<br /> }<br /> else if (ctr is HtmlInputText)<br /> {<br /> HtmlInputControl input = (HtmlInputControl)ctr;<br /> input.Attributes.Remove("readonly");<br /> input.Disabled = false;<br /> }</p><p> }