C# 更新时从GridView内的下拉列表中获取选定值
所以我有一些GridView。编辑行时,特定列将从标签更改为下拉列表。此下拉列表的内容通过一些SQL数据源填充。 用户可以选择并单击“更新” 如何才能真正获得下拉列表的SelectedValue属性 我认为这会奏效: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
<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
}