[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-08-27
9
Medium Priority
?
568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 33

Accepted Solution

by:
raterus earned 900 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 300 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

649 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