tags:

views:

57

answers:

2

Hi, I am trying to work thru some sample code. The code I am having a problem with is:

private ListControl GetSelectedList()
{
  return (ListControl)FindControl(ddlControls.SelectedItem.Value);
}

ddlControls is a DropDownListBoxControl collection What does the ddlControls.SelectedItem.Value return (its a numeric value, but I don't know what it represents)?

2nd question: What is return (ListControl)FindControl(ddlControls.SelectedItem.Value); ?

Thanks

+2  A: 

SelectedItem.Value, as the name suggests, is the value of the currently selected item in the drop-down list. For example, if this were a list of months and someone had selected "September", this property has the value "September".

what is return (ListControl)FindControl(ddlControls.SelectedItem.Value);

FindControl is a method that looks up controls by their id. Using our example from before, it would try to find a control with the name "September". The (ListControl) at the beginning is a cast; the GetSelectedList() method is implicitly assuming that every possible answer you could get from ddlControls is the name of another ListControl. (This might not be a good idea depending on the circumstances.)

The result -- that is, the control whose id is the same as the currently selected value in ddlControls -- is then returned, and that's the result of GetSelectedList().

John Feminella
John, thanks for your reply. I guess this ia a bit complex, but I think I understand the gist of what you are saying. Complex because this is probably a way to locate a control at runtime (which I have not done in the past). Thanks again.
LearningCSharp
+1  A: 

DropDownList Class in MSDN has the answer to the first question. In particular, it links to ListControl.SelectedItem, which is defined as:

If the list control allows only a single selection, use this property to get the individual properties of the selected item. If the list control allows multiple selections, use this property to get the properties of the lowest indexed item selected from the list control.

Similarly, Control.FindControl gives the answer to your second question. It's defined as:

Searches the current naming container for a server control with the specified id parameter.

Chip Uni