Solved

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

Posted on 2004-08-27
9
561 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 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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 …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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