Harry
Harry

Reputation: 2526

Get selected value from drop down inside GridView on Update

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

Answers (3)

Sravan Kumar
Sravan Kumar

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

Code52
Code52

Reputation: 56

You can try doing this:

assuming the ID of the gridview is gridview1

<asp:ControlParameter ControlID="gridview1$ServiceCategoriesGvDropDown" PropertyName="SelectedValue" />

Upvotes: 0

Icarus
Icarus

Reputation: 63956

What I think you need to do is:

  1. Attach a SelectedIndexChange event to the DropDownlist on your Gridview
  2. Grab the SelectedValue on that event.
  3. Grab a reference to your DataSource's UpdateParameters and populate the respective parameter programmatically with the SelectedValue.
  4. Call your DataSource's Update method.

Upvotes: 0

Related Questions