Click to See Complete Forum and Search --> : How do I set Dropdown.SelectedValue in EditItemTemplate?


Moik
May 19th, 2009, 10:54 AM
I have a Gridview in VS2008 that allows edit/update. I am using an EditItemTemplate to display a dropdown on edit instead of the usual label. This populates and works fine, but it does not remember the original value.

How do I set the initial selected value on the dropdownlist when it displays on edit? There does not seem to be a SelectedValue="" attribute in the dropdownlist and adding

<asp:ListItem><%# Bind("Description") %></asp:ListItem>

results in an error saying code blocks are not supported in this context.

Any clues would be welcomed

dannystommen
May 19th, 2009, 11:27 AM
Probably you need to use the FindControl method of your gridview (or gridview.Row Property)

(gridview1.FindControl("dropDownList1") as DropDownList).SelectedValue = "Something";

Moik
May 20th, 2009, 03:44 AM
Many thanks for the reply.

I have been trying something like that in C# called from onrowediting.

DropDownList editDD = (DropDownList)myGrid.FindControl("editDescription");
editDD.SelectedValue = "Something"

However when I try to set the value, editDD is null (I assume it can't find the control).

Also, I still have no idea how to get the "Something". How do I pass the original grid cell value into the code so that I can assign it to SelectedValue.

Thanks