GridView 中的 DropDownList 选择 GridView BoundField

发布于 2024-10-10 23:15:02 字数 767 浏览 2 评论 0原文

在 ASP.NET 4 中,我有一个 GridView,它在 asp:TemplateEdit 部分中包含一个 DropDownList。 DropDownList 填充了我需要从第二个数据源获取的所有选项。但是,我希望每行中的每个 DropDownList 根据 GridView 中已有的数据默认设置不同的 SelectedIndex。

例如,这里是示例数据

|  id  |     Status    |  hidden  |  First Name  |
--------------------------------------------------
|  12  |   <dropdown>  |     3    |  john  doe   |
|  13  |   <dropdown>  |     7    |  jane  ble   |

,因此您会注意到第三列是隐藏的,这意味着我将其设置为:

<asp:BoundField DataField="StatusName" HeaderText="Status" Visible="false" />

但是,此列中编辑的行包含我需要 DropDownList 默认显示的值 (asp:BoundField)。

换句话说,我需要

<option select="selected">something</option>

根据“隐藏”字段中的数字选择正确的索引。

In ASP.NET 4, I have a GridView, which contains a DropDownList in the asp:TemplateEdit section. The DropDownList is populating with all of the options I need from a second data source. However, I would like to have each DropDownList in each row have a different SelectedIndex set on default based on data already in the GridView.

For example, here is sample data

|  id  |     Status    |  hidden  |  First Name  |
--------------------------------------------------
|  12  |   <dropdown>  |     3    |  john  doe   |
|  13  |   <dropdown>  |     7    |  jane  ble   |

so you will note the 3rd column is hidden, meaning I have it set to:

<asp:BoundField DataField="StatusName" HeaderText="Status" Visible="false" />

However, the edited row in this column contains the value (asp:BoundField) that I need the DropDownList to display by default.

In other words I need

<option select="selected">something</option>

to have the proper index selected based on the number in the "hidden" field.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国粹 2024-10-17 23:15:02

您可以订阅 RowDataBound 事件,以便在下拉列表中选择正确的值:

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DropDownList lst = null;
    string hiddenStatus = string.Empty;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        hiddenStatus = e.Row.Cells[2].Text;
        lst = (DropDownList)e.Row.FindControl("dropDownIdentifier");

        lst.SelectedValue = hiddenStatus;
    }
}

You can subscribe to RowDataBound event in order to select the right value in the drop-down list:

protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DropDownList lst = null;
    string hiddenStatus = string.Empty;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        hiddenStatus = e.Row.Cells[2].Text;
        lst = (DropDownList)e.Row.FindControl("dropDownIdentifier");

        lst.SelectedValue = hiddenStatus;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文