ASP.net Gridview Itemtemplate下拉列表

时间:2012-04-20 02:08:40

标签: asp.net gridview itemtemplate html.dropdownlistfor

我正在使用C#ASP.net,并且相对较新,需要在下面完成。

在我的VS2010 Web应用程序项目中,我webform的{​​{1}}从表中获取数据。

在网格中,我有Gridview按钮(column1)和项目模板Commandfiled(第2列)。

用例是,用户首先从3个列表项(H,L和M)中选择一个Dropdownlist,然后选择命令按钮。

我无法弄清楚如何从选定的行中提取选定的listitem

listitem

提前致谢。

1 个答案:

答案 0 :(得分:2)

GridViewRow对象提供方法FindControl(与所有容器控件一样),以通过其id访问行中的控件。例如,如果您的DropDownList的ID为MyDropDown,则可以使用以下内容访问其所选值:

GridViewRow row = GridView2.SelectedRow;
DropDownList MyDropDown = row.FindControl("MyDropDown") as DropDownList;
string theValue = MyDropDown.SelectedValue;
// now do something with theValue

这是访问GridView中所有控件的推荐方法。您希望尽可能避免执行row.Cells[#]之类的操作,因为当您重新排列或添加/删除GridView中的列时,它很容易中断。