Most elegant way to clear all the text fields on asp.net page?

Discussion in 'ASP .Net' started by Duk Lee, Jul 23, 2007.

  1. Duk Lee

    Duk Lee Guest

    What is the most elegant way to clear all the text fields on asp.net
    page? I just don't think that

    txtShortName.Text = ""
    txtYearFounded.Text = ""
    txtCompanyCode.Text = ""
    txtCity.Text = ""
    txtOwnership.Text = ""
    txtAssetsUnderManagement.Text = ""
    txtNumberOfAnalysts.Text = ""
    txtTotalStaff.Text = ""
    txtCorporateOverview.Text = ""
    txtInvestmentProcess.Text = ""
    txtFirstName.Text = ""
    txtMiddleName.Text = ""
    txtLastName.Text = ""
    txtSuffix.Text = ""
    txtPosition.Text = ""
    txtNewSoftMinimum.Text = ""
    txtNewHardMinimum.Text = ""

    is a very smart way to do it.
     
    Duk Lee, Jul 23, 2007
    #1
    1. Advertising

  2. You could iterate through each control in a page using the Controls
    collection and then see if the current control is the type of a textbox.

    for (int i = 0; i < this.Controls.Count; i++)
    {
    if (this.Controls.GetType() == typeof(TextBox))
    ((TextBox)this.Controls).Text = string.Empty;

    }

    You may have to tweak this though to make sure you're getting the correct
    child controls. For example, instead of using the page or controls full
    control collection you may want to start with the htmlform control for the
    page or create a recursive mechanism to ensure you're getting all the
    controls in the page as this method will usually only load the top level
    controls in the hierarchy.


    --
    Hope this helps,
    Mark Fitzpatrick
    Microsoft FrontPage MVP 199?-2006. 2007 and beyond





    "Duk Lee" <> wrote in message
    news:...
    > What is the most elegant way to clear all the text fields on asp.net
    > page? I just don't think that
    >
    > txtShortName.Text = ""
    > txtYearFounded.Text = ""
    > txtCompanyCode.Text = ""
    > txtCity.Text = ""
    > txtOwnership.Text = ""
    > txtAssetsUnderManagement.Text = ""
    > txtNumberOfAnalysts.Text = ""
    > txtTotalStaff.Text = ""
    > txtCorporateOverview.Text = ""
    > txtInvestmentProcess.Text = ""
    > txtFirstName.Text = ""
    > txtMiddleName.Text = ""
    > txtLastName.Text = ""
    > txtSuffix.Text = ""
    > txtPosition.Text = ""
    > txtNewSoftMinimum.Text = ""
    > txtNewHardMinimum.Text = ""
    >
    > is a very smart way to do it.
    >
     
    Mark Fitzpatrick, Jul 23, 2007
    #2
    1. Advertising

  3. On Jul 23, 10:11 pm, Duk Lee <> wrote:
    > What is the most elegant way to clear all the text fields on asp.net
    > page? I just don't think that
    >
    > txtShortName.Text = ""
    > txtYearFounded.Text = ""
    > txtCompanyCode.Text = ""
    > txtCity.Text = ""
    > txtOwnership.Text = ""
    > txtAssetsUnderManagement.Text = ""
    > txtNumberOfAnalysts.Text = ""
    > txtTotalStaff.Text = ""
    > txtCorporateOverview.Text = ""
    > txtInvestmentProcess.Text = ""
    > txtFirstName.Text = ""
    > txtMiddleName.Text = ""
    > txtLastName.Text = ""
    > txtSuffix.Text = ""
    > txtPosition.Text = ""
    > txtNewSoftMinimum.Text = ""
    > txtNewHardMinimum.Text = ""
    >
    > is a very smart way to do it.


    foreach (Control c in Page.Controls)
    {
    foreach (Control cc in c.Controls)
    {
    if (cc is TextBox)
    {
    ((TextBox)cc).Text = String.Empty;
    }
    }
    }
     
    Alexey Smirnov, Jul 23, 2007
    #3
    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. Rob Cowie
    Replies:
    28
    Views:
    673
    Bruno Desthuilliers
    Jun 13, 2006
  2. Alex Buell

    What's the most elegant way of doing this?

    Alex Buell, Feb 3, 2009, in forum: C Programming
    Replies:
    3
    Views:
    348
    Alex Buell
    Feb 4, 2009
  3. Replies:
    32
    Views:
    327
    William James
    Nov 28, 2007
  4. Kenneth McDonald
    Replies:
    2
    Views:
    119
    Kenneth McDonald
    Sep 2, 2008
  5. Markus Dehmann
    Replies:
    6
    Views:
    153
    Michele Dondi
    Dec 14, 2007
Loading...

Share This Page