How to clear all textboxes on one page.

How to clear all textboxes on one page.

Page 1 of 1

6 Replies - 67286 Views - Last Post: 23 February 2012 - 03:57 AM Rate Topic: -----

#1 dmxxmd  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 30
  • Joined: 26-October 07

How to clear all textboxes on one page.

Post icon  Posted 13 November 2007 - 01:09 AM

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 ...

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 November 2007 - 04:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to clear all textboxes on one page.

#2 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Re: How to clear all textboxes on one page.

Posted 13 November 2007 - 01:58 AM

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... :D
But fiddle with what I've said so long.

Totsiens!
btw you might want to edit your post and add the [code] tags...
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to clear all textboxes on one page.

Posted 13 November 2007 - 04:23 PM

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

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

EmptyTextBoxes(this)




Hope this helps

This post has been edited by PsychoCoder: 13 November 2007 - 04:36 PM

Was This Post Helpful? 1

#10 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: How to clear all textboxes on one page.

Posted 14 November 2007 - 02:35 AM

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
<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
<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.
Was This Post Helpful? 2

#11 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: How to clear all textboxes on one page.

Posted 14 November 2007 - 02:55 AM

I think this post should be under ASP.NET?
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to clear all textboxes on one page.

Posted 14 November 2007 - 05:33 AM

You're right, and I don't know why I didn't move it last night, moved now :)
Was This Post Helpful? 0
  • +
  • -

#13 marooj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-January 09

Re: How to clear all textboxes on one page.

Posted 23 February 2012 - 03:57 AM

View PostNayana, on 14 November 2007 - 02:55 AM, said:

I think this post should be under ASP.NET?


i am write vb.net code i am change you code to vb.net thanks

Public Function EmptyTextBoxes(ByVal parent As Control)


For Each c As Control In parent.Controls


If TypeOf © Is TextBox Then
CType(c, TextBox).Text = ""
End If

If c.HasControls Then
EmptyTextBoxes©
End If
Next



End Function

View Postmarooj, on 23 February 2012 - 03:54 AM, said:

View PostNayana, on 14 November 2007 - 02:55 AM, said:

I think this post should be under ASP.NET?


i am write vb.net code i am change you code to vb.net thanks

Public Function EmptyTextBoxes(ByVal parent As Control)


For Each c As Control In parent.Controls


If TypeOf © Is TextBox Then
CType(c, TextBox).Text = ""
End If

If c.HasControls Then
EmptyTextBoxes©
End If
Next



End Function

And use the code in page-load or any event :
EmptyTextBoxes(me)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1