?
Solved

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

Posted on 2004-08-27
9
Medium Priority
?
565 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
Technology Partners: 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

Technology Partners: 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!

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

771 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