Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my aspx page contains 20 TextBoxes, i just want to clear all the textbox values.

I tried this code
C#
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...?
Posted
Comments
Ankur\m/ 13-Dec-13 4:47am    
Do you specifically want it on server side?
Ankur\m/ 13-Dec-13 4:49am    
Ok just saw the answer below by thatraja. It should answer your query.

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
C#
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);
              }
          }
      }
 
Share this answer
 
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
 
Share this answer
 
Comments
Ankur\m/ 13-Dec-13 4:49am    
That should help!
Tom Marvolo Riddle 13-Dec-13 5:03am    
Thanks for the links.5!
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 = "";
           }
       }
 
Share this answer
 
Hi Try this code..

Get the parent Container like Div or Panel or any.. and iterate from that.

Example:
XML
<div id="divContainer" runat="server">
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       '
       '
       '
       <asp:TextBox ID="TextBox20" runat="server"></asp:TextBox>


   </div>



Code Behind:

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (var item in divContainer.Controls)
            {
                if (item is TextBox)
                {
                    (item as TextBox).Text = "";
                }
            }
        }



If you want to do it in JQuery :

XML
<script type="text/javascript">

       var ClearAllTextBoxes = function () {

           $('input[type=text]').val('');

       }

   </script>
 
Share this answer
 
hi try this
C#
foreach (Control c in Panel2.Controls)
    {
        if (c is TextBox)
        {
            TextBox txt = (TextBox)c;
            txt.Text = "";
        }
    }
 
Share this answer
 
try this one


textbox1.Text ="";
 
Share this answer
 
if u want to reset then you can reload page like on click on a button u can redirect page like this

C#
Response.Redirect("register.aspx");

redirect on this page where your text box are
if it will reload then textbox will be empty
 
Share this answer
 
public void clear()
{
for(i=0; just write condition according counting or your textbox; i++)
{
your clear code
}
}
 
Share this answer
 
<asp:textbox id="TextBox1" runat="server" class="clear" xmlns:asp="#unknown">

Just put a class on all of your textboxes, then use Jquery Selector to clear all of them...

See:-

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>

$(function(){
$('.clear').val('');
});


Put this on your aspx page.

Hope, your problem will be solved..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900