【问题标题】:ASP.net Gridview Itemtemplate DropdownlistASP.net Gridview 项目模板下拉列表
【发布时间】:2012-04-20 02:08:39
【问题描述】:

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

在我的 VS2010 Web 应用程序项目中,我有 webformGridview,它从表中获取数据。

在网格中,我有 Commandfiled 按钮(column1)和带有Dropdownlist(column2)的项目模板。

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

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

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{

    GridViewRow row = GridView2.SelectedRow;
    Label4.Text = "You selected " + row.Cells[4].Text + "."; 
    Label5.Text = "You selected this list item from dropdownlist " + ???? ; 
}

提前致谢。

【问题讨论】:

    标签: asp.net gridview itemtemplate html.dropdownlistfor


    【解决方案1】:

    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 重新排列或添加/删除列时,它很容易中断。

    【讨论】:

    • 感谢先生的解决方案和避免单元格索引的提示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多