How: Get value of dropdownlist in a loop?
-
Ok, I can get the value of a dropdownlist buy doing this: dropdownlist1.selecteditem.value, i have left the default id for all the dropdownlists i have on the page. What I am trying to do is get the value in a loop. here is a simple example of what i am trying to do and what. dim temp as string for i=1 to 10 temp = dropdownlist(i).selecteditem.value next Obviously that didn't work so i tryed temp = dropdownlist"+cstr(i)+".selecteditem.value that also is not working, can anyone help. Thank you, Santana
-
Ok, I can get the value of a dropdownlist buy doing this: dropdownlist1.selecteditem.value, i have left the default id for all the dropdownlists i have on the page. What I am trying to do is get the value in a loop. here is a simple example of what i am trying to do and what. dim temp as string for i=1 to 10 temp = dropdownlist(i).selecteditem.value next Obviously that didn't work so i tryed temp = dropdownlist"+cstr(i)+".selecteditem.value that also is not working, can anyone help. Thank you, Santana
There might be a more efficient way, but you could loop through all of the controls on the page and check the type. If the control's type is dropdown list then retrieve the value Here is some psuedocode (C#) that should give you a basic idea of how to accomplish the task. foreach(Control c in Page.Controls) { if (c.GetType().ToString() == "System.Web.UI.WebControls.DropdownList") { Dropdownlist drp = (DropDownList) c; string retVal = c.selectedItem.value; } } I hope that helps.
-
There might be a more efficient way, but you could loop through all of the controls on the page and check the type. If the control's type is dropdown list then retrieve the value Here is some psuedocode (C#) that should give you a basic idea of how to accomplish the task. foreach(Control c in Page.Controls) { if (c.GetType().ToString() == "System.Web.UI.WebControls.DropdownList") { Dropdownlist drp = (DropDownList) c; string retVal = c.selectedItem.value; } } I hope that helps.
That's the way to do it. Since there is no such this as a control array in .NET, you have to iterate through the Page's Controls collection, comparing the type of each control to the one your looking for, then you can set a variable to that control, get its Name property and check if its the one you want. The only problem I have with what you wrote is that the type is completely dependent on the string. A better way would be something like this:
if( c.GetType() == typeof(DropDownList) )
This way, if the namespace string changes in future versions of the .NET Framework, you won't have to go back and rewrite your code with the new name. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome