当前位置:编程学习 > C#/ASP.NET >>

如何快速清除页面上所有TextBox和HtmlInput的值

我的做法行不通




 protected void ClearAllTextBox()
    {
        foreach (Control con in this.Controls)
        {
            try
            {
                TextBox edt = (TextBox)con; 
                edt.Text = string.Empty;
            }
            catch
            {
            }
            try
            {
                HtmlInputText edt = (HtmlInputText)con;
                edt.Value = string.Empty;
            }
            catch
            {
            }
        }
    }


--------------------编程问答-------------------- 用js清除。
因为textbox和HtmlInputText在客户端生成出来就是textarea和input[type=text],只要用js清除这两种标签的值就可以了。可以使用jQuery来做。 --------------------编程问答-------------------- 必须用递归的

 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 Button)
            {
                Button btn = (Button)ctr;
                btn.Enabled = false;

            }
            else if (ctr is FileUpload)
            {
                FileUpload btn = (FileUpload)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 CheckBoxList)
            {
                CheckBoxList cb = (CheckBoxList)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 if (ctr is HtmlInputFile)
            {
                HtmlInputFile input = (HtmlInputFile)ctr;
                input.Attributes.Add("readonly", "");
                input.Disabled = true;
            }
                
            else
                foreach (Control ctr1 in ctr.Controls)
                {
                    SetControlReadOnly(ctr1);
                }
        }
--------------------编程问答--------------------
引用 1 楼 junshanhudazhaxi 的回复:
用js清除。
因为textbox和HtmlInputText在客户端生成出来就是textarea和input[type=text],只要用js清除这两种标签的值就可以了。可以使用jQuery来做。


document.getElementsByName('text') --------------------编程问答--------------------
引用 1 楼 junshanhudazhaxi 的回复:
用js清除。
 因为textbox和HtmlInputText在客户端生成出来就是textarea和input[type=text],只要用js清除这两种标签的值就可以了。可以使用jQuery来做。

但是在服务端无法调用 jquery --------------------编程问答-------------------- 前台用js巨简单,为什么要在后台用那么复杂的方法呢?必须的吗? --------------------编程问答--------------------

private void ClearTextBox()
    {
        foreach (System.Web.UI.Control control in this.Controls)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                if (control.Controls[i] is System.Web.UI.WebControls.TextBox)
                {
                    System.Web.UI.WebControls.TextBox tbx = (System.Web.UI.WebControls.TextBox)control.Controls[i];
                    tbx.Text = "";
                }
            }
        }
    }

或者JavaScript

<script type="text/javascript" language="javascript">
         function ClearAllTextBox() {
             var obj = window.document.forms[0];
             for (i = 0; i < obj.elements.length; i++) {
                 var elem = obj.elements[i];
                 if (elem) {
                     if (elem.type == "text") {
                         elem.value = "";
                     }
                 }
             }
         }
     </script>
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,