Clear a form's Controls?

Discussion in 'ASP .Net Web Controls' started by Mark A. Deal, Jan 3, 2005.

  1. Mark A. Deal

    Mark A. Deal Guest

    Can anybody tell me how to iterate through the controls on a form and clear
    the textbox controls?

    I've tried:
    Dim objControl As Control

    For Each objControl In Me.Controls

    If TypeOf objControl Is TextBox Then

    TextBox.Text = ""

    End If

    Next

    Obviously does not work. Anybody?
    --
    Mark A. Deal
    Document & Data Solutions, LLC
    http://www.docsol.com
    Time Matters AIC
    HotDocs Certified Consultant
    GhostFill Certified Consultant
     
    Mark A. Deal, Jan 3, 2005
    #1
    1. Advertising

  2. Hi Mark,

    You're pretty close. Make sure you are getting to the Form controls, not
    just the Page controls

    Here's a sample:

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    TextBox1.Text = "Textbox1"
    TextBox2.Text = "Textbox2"
    TextBox3.Text = "Textbox3"
    End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    ' Clears the text out of textboxes
    ' Jan 3/05 by Ken Cox - Microsoft MVP [ASP.NET]
    Dim objTester As Control
    Dim txtbx As TextBox
    Dim frm As Control
    ' Get a reference to the form
    frm = Page.FindControl("Form1")
    ' Make sure the form exists
    If Not IsNothing(frm) Then
    ' Loop through all of the controls in the form
    For Each objTester In frm.Controls
    ' Test this control to see if it is
    ' a textbox
    If TypeOf (objTester) Is TextBox Then
    ' Assign the control to the textbox variable
    txtbx = objTester
    ' Remove the text
    txtbx.Text = ""
    End If
    ' Go back for more, if any
    Next
    End If
    End Sub

    <form id="Form1" method="post" runat="server">
    <p>
    <asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
    <p>
    <asp:textbox id="TextBox2" runat="server"></asp:textbox></p>
    <p>
    <asp:textbox id="TextBox3" runat="server"></asp:textbox></p>
    <p>
    <asp:button id="Button1" runat="server"
    Text="Clear"></asp:button></p>
    </form>

    Does this help?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto


    "Mark A. Deal" <> wrote in message
    news:...
    > Can anybody tell me how to iterate through the controls on a form and
    > clear
    > the textbox controls?
    >
    > I've tried:
    > Dim objControl As Control
    >
    > For Each objControl In Me.Controls
    >
    > If TypeOf objControl Is TextBox Then
    >
    > TextBox.Text = ""
    >
    > End If
    >
    > Next
    >
    > Obviously does not work. Anybody?
    > --
    > Mark A. Deal
    > Document & Data Solutions, LLC
    > http://www.docsol.com
    > Time Matters AIC
    > HotDocs Certified Consultant
    > GhostFill Certified Consultant
    >
    >
     
    Ken Cox [Microsoft MVP], Jan 3, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chrisk

    controls.clear()

    chrisk, Aug 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,504
    chrisk
    Aug 22, 2003
  2. Ricardo Corsi P. Cesar

    How can i do to clear all the textbox using controls?

    Ricardo Corsi P. Cesar, Nov 21, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    531
    Kiran Kumar
    Nov 21, 2003
  3. =?Utf-8?B?bmF5?=

    Clear view state from group of controls

    =?Utf-8?B?bmF5?=, Apr 14, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    395
    =?Utf-8?B?bmF5?=
    Apr 14, 2004
  4. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,070
    Mark Fitzpatrick
    Jan 31, 2008
  5. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    1,009
    Steven Cheng[MSFT]
    Mar 6, 2007
Loading...

Share This Page