Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I have a webpage with some text boxes and one button with name refresh, when I click the button all the textboxes should get refresh, how can u achieve this?

In button event we can write the textbox1.text=”” or text.clear();


Side question if I have 25 control in my webform then for 25 controls you will write text. text=”” ------ so on.


Thank you
Posted
Updated 21-Jan-15 23:24pm
v2
Comments
tulasiram.v 22-Jan-15 5:16am    
refresh means clearing data inside text box or what?if yes
under button click event write below line of code
txt1.Text=txt2.Text=..="";
can you explore your question..
Zoltán Zörgő 22-Jan-15 5:17am    
What do you mean by refresh? Get some data from server?
arvind mepani 22-Jan-15 5:18am    
What do you mean by refresh ? Is page need to postback or just javascript Click event. provide specific problem you have.
Black_Rose 22-Jan-15 5:24am    
This was asked to me in one interview.i updated with the entire question.
tulasiram.v 22-Jan-15 5:43am    
we are asking about refresh meaning are you want load controls again use javascript function and write code like window.location.reload() it will refresh entire page including all your controls

you can use javascript to reset the form:

JavaScript
document.getElementById('form1').reset();
on button click.

where 'form1' is the form id.
 
Share this answer
 
Since this is web page, a clear all operation should not have to incur a round trip all the way to the server and back. A simple jQuery will suffice, e.g.
XML
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $("#<%= Button1.ClientID %>").click(function () {
            $("input:text").val("");
        });
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Refresh" />
    </div>
    </form>
</body>
</html>
 
Share this answer
 
v2
you could reload the page from client side, then you will get new page with empty text boxes
sample code:
HTML
<body>
 First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
<button onclick="myFunction()">Refresh</button>

<script>
function myFunction() {
    location.reload();
}
</script>

or clear the textbox values from javascript
HTML
<body>
 First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
<button onclick="myFunction()">Refresh</button>

<script>
function myFunction() {
    var elements = document.getElementsByTagName("input");
    for (var ii=0; ii < elements.length; ii++) {
      if (elements[ii].type == "text") {
        elements[ii].value = "";
      }
   }
}
</script>

client side textbox clearing will save round trip to server side.
 
Share this answer
 
v3
I guess you want to clear your all textbox value on single button click.
And you don't want to write hundred lines of code.

you can try this code :

C#
protected void bthClearAll_Click(object sender, EventArgs e)
{
    ClearInputs(Page.Controls);
}
void ClearInputs(ControlCollection ctrls)
{
    foreach (Control ctrl in ctrls)
    {
        if (ctrl is TextBox)
            ((TextBox)ctrl).Text = string.Empty;
        ClearInputs(ctrl.Controls);
    }
}


Reference : How to clear a Multiple TextBox values in a single click in C# .NET[^]

And now the question is if you don't want to clear all the text box. Suppose you want to clear only 40 textbox out of 80. then you can use these codes :

C#
protected void bthClearAll_Click(object sender, EventArgs e)
   {
       var textboxlist = new List<TextBox>() { TextBox1, TextBox2, TextBox3 };//this contains lists of ids of your textbox you want to clear.
       foreach (var textBox in textboxlist )
       {
           textBox.Text = "";
       }
   }



Good luck.
 
Share this answer
 
v2
use javascript to do this :

Create function and call it on button click:

like

C#
function Clear()
{
   document.getElementById("txt1").value= "";
   document.getElementById("txt2").value= "";
   document.getElementById("txt3").value= "";
   document.getElementById("txt4").value= "";
}
 
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