【问题标题】:Getting pre-selected Text in dropdown in EditItemTemplate gridview在 EditItemTemplate gridview 的下拉列表中获取预选文本
【发布时间】:2013-07-01 05:37:27
【问题描述】:

我无法在编辑模板的下拉列表中获得预选文本。请看我的代码:

<EditItemTemplate>
   <asp:DropDownList ID="droplist" runat="server">
   </asp:DropDownList>
</EditItemTemplate>

c#代码

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList droplist = (DropDownList)e.Row.FindControl("droplist");
                droplist.DataSource = EquipmentBLL.getunitdrop();
                droplist.DataTextField = "UnitName";
                droplist.DataValueField = "UnitID";
                droplist.DataBind();
                droplist.Items.Insert(0, new ListItem(" Select Unit ", "0"));
                //droplist.Items.FindByText(unittypetext).Selected = true;
            }
        }
}

谁能告诉我我应该怎么做才能获得预选的下拉菜单?

问候 侯赛因

【问题讨论】:

  • 你有什么错误吗?
  • 当我点击编辑按钮下拉菜单时不需要错误,应该在所有预选的行项目旁边预先选择该特定行的数据库值。

标签: c# asp.net gridview drop-down-menu edititemtemplate


【解决方案1】:

现在,您正在使用来自数据源的选项填充DropDownList。但是,您没有将它的选定值绑定到任何东西。

无论您正在做什么来绑定Gridview 中的其他字段,也要对DropDownList 的 SelectedValue 执行此操作。

没有看到你的 GridView 标记的其余部分,我认为这样的东西应该可以工作:

<EditItemTemplate>
   <asp:DropDownList ID="droplist" runat="server" 
        SelectedValue='<%# Bind("UnitID") %>' >
   </asp:DropDownList>
</EditItemTemplate>

上面的“UnitID”是您要绑定到 DropDownList 的 SelectedValue 的 GridView 数据源中的字段名称。

【讨论】:

    猜你喜欢
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多