Solved

Loop through all TextBoxes on page and display Text value (C#)

Posted on 2004-08-27
9
542 Views
Last Modified: 2012-08-13
I have several different types of forms and controls on a page.  How do I loop through all the TextBoxes and DropDownLists and display their respective Value/Text.  I tried this, but it doesn't work:

foreach (Control c in Page.FindControl("form").Controls)
{
    if( c is WebControl ) Response.Write(( (WebControl) c ).Text);
    else if( c is HtmlControl ) Response.Write(( (HtmlControl) c ).Text);
}

I get the error "'System.Web.UI.WebControls.WebControl' does not contain a definition for 'Text'".  How can I reference the Text value of a TextBox control and the Value of a DropDownList?
0
Comment
Question by:hendridm
  • 4
  • 4
9 Comments
 
LVL 33

Accepted Solution

by:
raterus earned 225 total points
ID: 11917432
You have to specifically cast it to a Textbox before grabbing the text value, not all WebControls have a "text" property.

foreach (Control c in Page.FindControl("form").Controls)
{
    if( c is TextBox ) Response.Write(( (TextBox) c ).Text);
}

Also your code is a bit flawwed in that it will only get textboxes 1 level deep within your form, that means if you had a textbox embedded within a panel, this code won't get it.  If that poses a problem, just let me know and I'll write out what you need to do.
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 75 total points
ID: 11917444
public void DisplayControls(ControllCollection c) {
    if(c.Controls.Count > 0) {
          DisplayControls(c) ;
     }
 
    foreach(Control Current in c) {
          if( c is TextBoxl ) Response.Write(( (TextBox) c ).Text);
    }
}

this will do TextBoxes ... you would just use DropDownList.SelectedVlue for drop downs.

you can also access these values through the Request object as they are passed back up in the form.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11917449
too slow I guess.
0
 
LVL 33

Expert Comment

by:raterus
ID: 11917453
nevermind about that last comment, I didn't look at your code hard enough and missed the "findcontrol"... :-)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 33

Expert Comment

by:raterus
ID: 11917499
public void DisplayControls(ControllCollection c) {
    if(c.HasControls()) {
          DisplayControls(c) ;
     }
 
    foreach(Control Current in c) {
          if( c is TextBoxl ) Response.Write(( (TextBox) c ).Text);
    }
}

Yea that's right foo, you're too slow and so is your code, this is slightly more efficient, since you don't need to compare the count to a scalar value.
<SLAP!>
..sorry it's friday :-)
0
 
LVL 33

Expert Comment

by:raterus
ID: 11917568
nevermind..you can slap me for being stupid now...(forget everything I said, a controlcollection doesn't have a hascontrols property)
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11917605
thats ok I typed mine too quickly too ...

public void DisplayControls(ControllCollection c) {
    foreach(Control Current in c) {
          if( Current is TextBoxl ) Response.Write(( (TextBox) Current ).Text);
            if(Current.HashControls) {
                  DisplayControls(Current.Controls) ;
            }

    }
}

is I believe what we both really meant.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11917607
hashcontrol hascontrols
0
 
LVL 8

Author Comment

by:hendridm
ID: 11917615
Doh!  I found my problem.  Stupid mistake - it's friday :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now