0

I have a simple web form which consists of several text box and drop downs. I need a way to get the value of all of the elements from the start in a list. I know I can individually get the value using textbox1.value but I wouldn't know how many textbox/dropdown there are in the form. For example

<form id="form1" runat="server">
    <div>
        Tier: 
            <asp:DropDownList ID="Tier" runat="server">
                        <asp:ListItem>Select Tier</asp:ListItem>
                        <asp:ListItem>Tier1</asp:ListItem>
                        <asp:ListItem>Tire2</asp:ListItem>
                        <asp:ListItem>Tier3</asp:ListItem>
            </asp:DropDownList>

        Author: 
            <asp:DropDownList ID="Author" runat="server">
                        <asp:ListItem>Select Author</asp:ListItem>
                        <asp:ListItem>Author1</asp:ListItem>
                        <asp:ListItem>Author2</asp:ListItem>
                        <asp:ListItem>Author3</asp:ListItem>
            </asp:DropDownList>

        Quotation For:
            <asp:TextBox ID="questionfor" runat="server"></asp:TextBox>
    </div>

How do I get the value in a list ? How do I loop through them ? The final aim is to store the element Id and its value in the database. Hence along with the value I need the id as well. For example in the database I will have two column elementName(which is the elementID) and its corresponding value.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user3342812
  • 343
  • 8
  • 25
  • 1
    you can do this several ways.. if you understand `Lambda's` and how to do a foreach you can do this based on the Controls type.. or you could do it the old fashion way and use a `foreach(Control ctrl in Controls)` checking if typs is `DropDowList` or TextBox..etc.. pretty simple actually – MethodMan Feb 29 '16 at 19:43

1 Answers1

0

here is something you can try

Protected void GetControlValues(ControlCollection  controls)
{
    var holdList = new List<string>();
    foreach(Control c in controls)
    {
        if(c is System.Web.UI.WebControls.TextBox)
        {
            holdList.Add(c.Text); //this will get the value of the TextBox 
        } 
        else if (c.controls.Count > 0)
        {
            GetControlValues(c.Controls)
        }
    }
}

if you want to get the ID value then you could check if(c.ID == textBoxId.ID) then add that textBoxId.Text to the List you can have multiple if checks inside this foreach loop if you know the types of controls you are checking..

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Where is the recursion? – Uwe Keim Feb 29 '16 at 19:53
  • BTW: _No_ control is of type `ListItem`. – Uwe Keim Feb 29 '16 at 19:54
  • I will edit the answer to show recursion using a foreach(Controls loop – MethodMan Feb 29 '16 at 19:56
  • why just dropdownlist? Will this also take care of textbox? or for that matter any other elements like checkbox, etc ? – user3342812 Feb 29 '16 at 19:57
  • you need you can do this for every control if you check the types using a `foreach(Control ctrl in Controls)` you need to check check using `if(c is TextBox)` for example.. it's pretty straight forward ..I will do edit and show you how using a `TextBox` then it will make more sense – MethodMan Feb 29 '16 at 20:04
  • there seems to be something wrong with controls. It shows count as 5 when i have more than 5 elements. And they are represented at {System.Web.UI.LiteralControl} – user3342812 Feb 29 '16 at 20:15
  • then you need to change the code to check if it's `System.Web.UI.LiteralControl` – MethodMan Feb 29 '16 at 20:16