Pass DropDownList selected value to Label - FindControl problem?
-
I want to pass the value of the selected item in a dropdownlist to a label. The dropdownlist is inside a FormView and the label is outside in the main form. I'm trying to do this by implementing an event handler for the event OnSelectedIndexChanged. I cannot get it to work as I get an error: Object reference not set to an instance of an object w.r.t code: Label1.Text = myDDL.SelectedItem.Value.ToString(); Here's the ASP/HTML
... .... .... .... .... < ...
Here's the event handler (c#) protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); Label1.Text = myDropDownList.SelectedItem.Value.ToString(); } My FindControl clearly isn't doing what I think it should. I can get this to work if the dropdownlist is not inside a FormView where there is no need to use FindControl. E.g. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Value.ToString(); } But unfortunately I NEED to have the dropdownlist inside a FormView. Thanks Majella
-
I want to pass the value of the selected item in a dropdownlist to a label. The dropdownlist is inside a FormView and the label is outside in the main form. I'm trying to do this by implementing an event handler for the event OnSelectedIndexChanged. I cannot get it to work as I get an error: Object reference not set to an instance of an object w.r.t code: Label1.Text = myDDL.SelectedItem.Value.ToString(); Here's the ASP/HTML
... .... .... .... .... < ...
Here's the event handler (c#) protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); Label1.Text = myDropDownList.SelectedItem.Value.ToString(); } My FindControl clearly isn't doing what I think it should. I can get this to work if the dropdownlist is not inside a FormView where there is no need to use FindControl. E.g. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Value.ToString(); } But unfortunately I NEED to have the dropdownlist inside a FormView. Thanks Majella
Your drop down list will be rendered on the client with a name like FormViewName_DropDownList1.
-
I want to pass the value of the selected item in a dropdownlist to a label. The dropdownlist is inside a FormView and the label is outside in the main form. I'm trying to do this by implementing an event handler for the event OnSelectedIndexChanged. I cannot get it to work as I get an error: Object reference not set to an instance of an object w.r.t code: Label1.Text = myDDL.SelectedItem.Value.ToString(); Here's the ASP/HTML
... .... .... .... .... < ...
Here's the event handler (c#) protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); Label1.Text = myDropDownList.SelectedItem.Value.ToString(); } My FindControl clearly isn't doing what I think it should. I can get this to work if the dropdownlist is not inside a FormView where there is no need to use FindControl. E.g. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Value.ToString(); } But unfortunately I NEED to have the dropdownlist inside a FormView. Thanks Majella
How about something like this?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList myDropDownList = (sender as DropDownList);
if (myDropDownList != null)
Label1.Text = myDropDownList.SelectedItem.Value.ToString();
} -
Your drop down list will be rendered on the client with a name like FormViewName_DropDownList1.
Thanks - I had omitted to specify the container (FormView1). Was DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); but should have been DropDownList myDropDownList = (DropDownList)FormView1.FindControl("DropDownList1"); Many thanks Majella