?
Solved

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

Posted on 2004-08-27
9
Medium Priority
?
571 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 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
Independent Software Vendors: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 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