Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,094 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,610 people online right now. Registration is fast and FREE... Join Now!




How to clear all textboxes on one page.

 
Reply to this topicStart new topic

How to clear all textboxes on one page., How to clear all textboxes on one page.

dmxxmd
13 Nov, 2007 - 12:09 AM
Post #1

New D.I.C Head
*

Joined: 25 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
Hi there
Can someone please help me with this piece of code, I would like to clear all my textboxes at once with one click on a button..Im creating a web app so I cannot use system.windows.forms
I believe it will be system.web ..... Im quite new in building webapps please can someone help me with this... I will appriciate it.. Here is the code ...

CODE

protected void ButtonAddRecord_Click(object sender, EventArgs e)
    {
        ClearForm(this);
    }
    public static void ClearForm(System.Windows.Forms.Control parent)
    {
        foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
        {
            //Loop through all controls
            if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
            {
                //Kyk of dit textbox is
                //as dit is maak die textbox empty
                ((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
                

            }
        }
    }
}


This post has been edited by PsychoCoder: 13 Nov, 2007 - 03:28 PM
User is offlineProfile CardPM
+Quote Post

Footsie
RE: How To Clear All Textboxes On One Page.
13 Nov, 2007 - 12:58 AM
Post #2

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 303



Thanked: 4 times
Dream Kudos: 50
My Contributions
I think what you are looking for is System.Web.UI.WebControls.TextBox
Also I think that if you haven't saved anything into ViewState and you page Posts Back, all your TextBoxes will clear anyway. This is because web pages are stateless and you have to explicitly tell them to maintain their state.

Maybe one of the pros can comment here as well... biggrin.gif
But fiddle with what I've said so long.

Totsiens!
btw you might want to edit your post and add the [code] tags...

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Clear All Textboxes On One Page.
13 Nov, 2007 - 03:23 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Heres a small snippet I use to empty all the textboxes on a web form:

CODE

public void EmptyTextBoxes(Control parent)
{
    // Loop through all the controls on the page
    foreach (Control c in parent.Controls)
    {
        // Check and see if it's a textbox
        if ((c.GetType() == typeof(TextBox)))
        {
            // Since its a textbox clear out the text    
            ((TextBox)(c)).Text = "";
        }
        // Now we need to call itself (recursive) because
        // all items (Panel, GroupBox, etc) is a container
        // so we need to check all containers for any
        // textboxes so we can clear them
        if (c.HasControls) {
            EmptyTextBoxes(c);
        }
    }
}


To use it, call it like this

CODE

EmptyTextBoxes(this)



Hope this helps

This post has been edited by PsychoCoder: 13 Nov, 2007 - 03:36 PM
User is online!Profile CardPM
+Quote Post

Nayana
RE: How To Clear All Textboxes On One Page.
14 Nov, 2007 - 01:35 AM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
If all you're doing is clearing the text boxes on your web page, you don't really want to do a post back, do it in JavaScript instead.

E.g. (Javascript)
Go to the HTML editor in Visual Studio (if you are using that, it's a tab at the bottom of the design screen), otherwise, edit the .ASPX file

put this code inside the <head> element
CODE

<script language="javascript" type="text/javascript">
function clearinputs(sType) {
  a = document.getElementsByTagName("input");
  for(i = 0; i < a.length; i++) {
    if(a[i].type==sType) {
      a[i].value = "";
    }
  }
}
</script>


and somewhere else (where you want the button), but this code
CODE
<button onclick="clearinputs('text');">Reset</button>


And then you won't need a post-back (wasting precious client/server time)

I know this isn't C#, but if you're doing web programming, you better learn HTML and JavaScript.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: How To Clear All Textboxes On One Page.
14 Nov, 2007 - 01:55 AM
Post #5

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
I think this post should be under ASP.NET?

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Clear All Textboxes On One Page.
14 Nov, 2007 - 04:33 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You're right, and I don't know why I didn't move it last night, moved now smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:28PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month