Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Duk Lee - 23 Jul 2007 21:11 GMT
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 - 23 Jul 2007 21:22 GMT
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[i].GetType() == typeof(TextBox))
       ((TextBox)this.Controls[i]).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.

Signature

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

> What is the most elegant way to clear all the text fields on asp.net
> page? I just don't think that
[quoted text clipped - 18 lines]
>
> is a very smart way to do it.
Alexey Smirnov - 23 Jul 2007 21:25 GMT
> What is the most elegant way to clear all the text fields on asp.net
> page? I just don't think that
[quoted text clipped - 18 lines]
>
> 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;
       }
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.