Reputation: 2526
So I have some GridView. When you edit a row, a particular column changes from a label to a DropDownList. The content of this drop down is populated via some SQL data source. The user may make a selection choice and click "Update".
How can I actually get at the SelectedValue property of the drop down?
I thought this would work:
<asp:GridView ... >
<Columns>
...
<EditItemTemplate>
<asp:DropDownList ID="ServiceCategoriesGvDropDown" AutoPostBack="True" .../>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ... />
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
And then wire it up with a ControlParameter in my SqlDataSource:
<UpdateParameters>
...
<asp:ControlParameter ControlID="ServiceCategoriesGvDropDown" PropertyName="SelectedValue" ... />
</UpdateParameters>
However, I get the following exception:
System.InvalidOperationException: Could not find control 'ServiceCategoriesGvDropDown' in ControlParameter 'ServiceCategoriesID'.
So clearly my drop down doesn't get found. Perhaps it's been destroyed by this point?
Upvotes: 2
Views: 12393
Reputation: 1457
try this in the updating event of the grid.
protected void YourGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList ddl= (DropDownList )YourGrid.Rows[e.RowIndex].FindControl("ddlId");
string selectedvalue=ddl.selectedvalue;
//Your update code goes here
}
Upvotes: 3
Reputation: 56
You can try doing this:
assuming the ID of the gridview is gridview1
<asp:ControlParameter ControlID="gridview1$ServiceCategoriesGvDropDown" PropertyName="SelectedValue" />
Upvotes: 0
Reputation: 63956
What I think you need to do is:
SelectedIndexChange
event to the DropDownlist
on your Gridview
SelectedValue
on that event.UpdateParameters
and populate the respective parameter programmatically with the SelectedValue
.Update
method.Upvotes: 0