Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have created a simple registration page, I want to clear all the textboxes that are present in the html table. Could any one provide solution for this

XML
<table id="tblReg">
        <tr><td>User Name</td>
            <td>:</td>
            <td><asp:TextBox ID="txtUName" runat="server" /></td>
        </tr>
        <tr><td>Email</td>
            <td>:</td>
            <td><asp:TextBox ID="txtEmail" runat="server" /></td>
        </tr>
        <tr>
            <td>Password</td>
            <td>:</td>
            <td><asp:TextBox ID="txtPwd" runat="server" TextMode="Password" /></td>
        </tr>
        <tr>
            <td>Confirm Password</td>
            <td>:</td>
            <td><asp:TextBox ID="txtCPwd" runat="server" TextMode="Password" /></td>
        </tr>
        <tr>
            <td></td>
            <td><asp:Button ID="btnReg" runat="server" OnClick="btnReg_Click" Text="Click"/></td>&nbsp;&nbsp;
            <td><asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="Cancel"/></td>
        </tr>
    </table>


on Cancel button's click event I have written the following, but its not working...

C#
protected void btnCancel_Click(object sender, EventArgs e)
    {
        foreach (Control c in this.Controls)
        {

            if (c is TextBox)
            {
                ((TextBox)c).Text = String.Empty;
            }
        }
    }
Posted
Comments
Sergey Alexandrovich Kryukov 2-Jun-15 2:35am    
Why on server side?
—SA

Your problem is that the text boxes in questions are not immediate children of this.Controls. You could do something else, using recursion, but it would make little sense.

It's a bad idea to do such a simple thing on a server side; you can do it, but the server callback in each tiny action is something which kills the performance and waste the bandwidth by so many Web sites.

You can easily do it on the client site in no time. The idea is simple: in JavaScript, find all the input elements by their IDs or by type, or combination of the element and input type attribute and, for the set of found controls, assign their value attributes to empty string.

It's very convenient to use jQuery for such things, where such problems are already solved. It can look something like this:
JavaScript
var textBoxSet = $("input, [type='text']"); // assuming you want to cover 
                                            // all <input type="text">
                                            // elements
// you can use more complex selector to select only special cases
// of such elements, say, of one specific table,
// or using specific CSS class...

textBoxSet.each(function(index, element){
   element.val('');
});


Please see:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement[^],
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-type[^],
https://api.jquery.com/element-selector[^],
https://api.jquery.com/attribute-equals-selector[^],
https://api.jquery.com/element-selector[^];
http://api.jquery.com/jquery.each[^],
http://api.jquery.com/val[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery,
http://jquery.com,
http://learn.jquery.com,
http://learn.jquery.com/using-jquery-core,
http://learn.jquery.com/about-jquery/how-jquery-works (start from here).

—SA
 
Share this answer
 
Go for this solutions : Clear all textbox in C#
 
Share this answer
 
Hi bhvch,

I would rather use javascript or jQuery for clearing the textboxes instead of doing a server call with the Cancel button.

Remove the OnClick event and add OnClientClick event as following:

OnClientClick="return ClearAll"
and add the below code in you web page inside script tags. I hope you know how to use the javascript.

C#
function ClearAll() {
    var objInput = document.getElementsByTagName("INPUT");
    for (var iCount = 0; iCount < objInput.length; i++) {
        if (objInput[iCount].type == "text")
            objInput[iCount].value = "";
    }
    return false;
}


This is a more efficient way of handling this scenario.

jQuery is even better but you need to add jQuery file references to your page.

Thanks,
RelicV
 
Share this answer
 
Hi bhvch,


try to this code,
C#
protected void btnCancel_Click(object sender, EventArgs e)
   {
       LoopTxtBoxes(Page.Controls);
   }
   private void LoopTxtBoxes(ControlCollection controlCollection)
   {
       foreach (Control control in controlCollection)
       {
           if (control is TextBox)
           {
               ((TextBox)control).Text = "";
           }

           if (control.Controls != null)
           {
               LoopTxtBoxes(control.Controls);
           }
       }
   }
 
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