如何清除TextBox值 [英] How to clear TextBox values

查看:113
本文介绍了如何清除TextBox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的aspx页面中包含20个TextBox,我只想清除所有文本框值。



我试过这段代码

< pre lang =c#> textbox1.text = ;
textbox2.text = ;
textbox3.text = ;
textbox4.text = ;
textbox5.text = ;
.......



但如果我们有50个以上的控件,那就不正确......



有没有其他选择...?

解决方案

您可以创建一个方法来重置表单,但为什么只能用于文本框。我建议创建一个方法来重置下拉列表,Checkbox,RadioButton和List

看到这个

  public   static   void  ResetFormControlValues(Control parentControlId)
{
foreach (控制c in parentControlId.Controls)
{
if (c.Controls.Count < = 0
{
switch (c.GetType()。ToString())
{
case System.Web.UI.WebControls.TextBox
((TextBox)c).Text = string .Empty;
break ;
case System.Web.UI。 WebControls.CheckBox
((CheckBox)c).Checked = false ;
break ;
case System.Web.UI。 WebControls.RadioButton
((RadioButton)c).Checked = false ;
break ;
case System.Web.UI。 WebControls.DropDownList
((DropDownList)c).SelectedIndex = 0 ;
break ;
case System.Web.UI。 WebControls.ListBox
foreach (ListItem item in ((ListBox) c).Items)
{
item.Selected = false ;
}

break ;
}
}
else
{
ResetFormControlValues(c);
}
}
}


查看此提示/技巧

清除所有输入字段的简单方法在表格中。 [ ^ ] - 服务器端

清除表单中所有输入字段的简单方法。 [ ^ ] - 客户端



并根据您的要求自定义代码


查找给定面板中的所有文本框并将其设置为空或空白

请参阅以下代码

 foreach(在PanelofAllTextBox.Controls中控制ctrl)
{
if(ctrl.GetType()。Name.ToString()==TextBox)
{
TextBox txt =(TextBox)ctrl;
txt.Text =;
}
}


In my aspx page contains 20 TextBoxes, i just want to clear all the textbox values.

I tried this code

textbox1.text ="";
textbox2.text ="";
textbox3.text ="";
textbox4.text ="";
textbox5.text ="";
.......


But its not correct if we have 50 and more controls......

Is there any alternative...?

解决方案

You can Create a method to reset the Form but why only for textbox. I would suggest to create a method to reset dropdown, Checkbox, RadioButton and List
see this

public static void ResetFormControlValues(Control parentControlId)
      {
          foreach (Control c in parentControlId.Controls)
          {
              if (c.Controls.Count <= 0)
              {
                  switch (c.GetType().ToString())
                  {
                      case "System.Web.UI.WebControls.TextBox":
                          ((TextBox)c).Text = string.Empty;
                          break;
                      case "System.Web.UI.WebControls.CheckBox":
                          ((CheckBox)c).Checked = false;
                          break;
                      case "System.Web.UI.WebControls.RadioButton":
                          ((RadioButton)c).Checked = false;
                          break;
                      case "System.Web.UI.WebControls.DropDownList":
                          ((DropDownList)c).SelectedIndex = 0;
                          break;
                      case "System.Web.UI.WebControls.ListBox":
                          foreach (ListItem item in ((ListBox)c).Items)
                          {
                              item.Selected = false;
                          }

                          break;
                  }
              }
              else
              {
                  ResetFormControlValues(c);
              }
          }
      }


Check this Tip/Tricks
Simple and easy way to clear all the input fields in the form.[^] - Server side
Simple and easy way to clear all the input fields in the form.[^] - client side

And customize the code based on your requirement


Find all textboxes from a given panel and Make them to Null or Blank
See the following code

foreach (Control ctrl in PanelofAllTextBox.Controls)
       {
           if (ctrl.GetType().Name.ToString() == "TextBox")
           {
               TextBox txt = (TextBox)ctrl;
               txt.Text = "";
           }
       }


这篇关于如何清除TextBox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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