設定頁面所有控制項唯讀。

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.