Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Selected Value Of A Control By Reference

Status
Not open for further replies.

SaturnSeven

Programmer
Aug 4, 2005
40
GB
Hi
I'm trying to get the selected value of a dropdownlist by using a reference to the control i.e. a variable will determine what dropdownlist to use for the selected value

Here my working so far

i = right(ctlChoice.ID,1)
strControlName = "SizeTo" & i
DDL = CType(FindControl(strControlName), DropDownList).SelectedItem.Value

I get NullReferenceException was unhandled by user code on the DDL = line

Any help would be greatly appreciated
Many thanks
 
if you cannot access the control direction
MyDropDownList.SelectedValue;
then you need to recurse through the controls to find it.
GetListControl("MyDropDownList", this).SelectedValue;
Code:
private ListControl GetListControl(string controlId, Control parent)
{
   Control found = parent.FindControl(controlId);
   if(found != null)
      return (ListControl)found;
   foreach(Control child in parent.Controls)
   {
        found = GetListControl(controlId, child);
        if(found != null)
           return (ListControl)found;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
another point. if a value has not been selected an exception will be thrown.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top