Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新时从GridView内的下拉列表中获取选定值_C#_Asp.net_Gridview_Drop Down Menu - Fatal编程技术网

C# 更新时从GridView内的下拉列表中获取选定值

C# 更新时从GridView内的下拉列表中获取选定值,c#,asp.net,gridview,drop-down-menu,C#,Asp.net,Gridview,Drop Down Menu,所以我有一些GridView。编辑行时,特定列将从标签更改为下拉列表。此下拉列表的内容通过一些SQL数据源填充。 用户可以选择并单击“更新” 如何才能真正获得下拉列表的SelectedValue属性 我认为这会奏效: <asp:GridView ... > <Columns> ... <EditItemTemplate> <asp:Dr

所以我有一些GridView。编辑行时,特定列将从标签更改为下拉列表。此下拉列表的内容通过一些SQL数据源填充。 用户可以选择并单击“更新”

如何才能真正获得下拉列表的SelectedValue属性

我认为这会奏效:

    <asp:GridView ... >
        <Columns>
            ...
                <EditItemTemplate>
                    <asp:DropDownList ID="ServiceCategoriesGvDropDown" AutoPostBack="True" .../>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ... />
                </ItemTemplate>
            </asp:TemplateField>
            ...
        </Columns>
    </asp:GridView>   

...
...
然后将其与我的SqlDataSource中的ControlParameter连接起来:

    <UpdateParameters>
        ...
        <asp:ControlParameter ControlID="ServiceCategoriesGvDropDown" PropertyName="SelectedValue" ... />
    </UpdateParameters>

...
但是,我得到以下例外情况:

System.InvalidOperationException:在控制参数“ServiceCategoriesID”中找不到控件“ServiceCategoriesGvDropDown”


很明显,我的下拉列表找不到。也许它已经被这一点摧毁了?

我想你需要做的是:

  • SelectedIndexChange
    事件附加到
    Gridview上的
    DropDownlist
  • 抓取该事件的
    SelectedValue
  • 获取对数据源的
    UpdateParameters
    的引用,并使用
    SelectedValue
    以编程方式填充相应的参数
  • 调用数据源的
    Update
    方法

  • 您可以尝试这样做:

    假设gridview的ID为gridview1

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

    在网格的更新事件中尝试此操作

     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
     }