5

在我的 Gridview 中,我有以下模板字段:

<asp:TemplateField HeaderText="Dept Code" SortExpression="DeptCode">
    <ItemTemplate>
        <%# Eval("DeptCode") %>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList ID="ddlDeptCode" runat="server" 
            SelectedValue='<%# Eval("DeptCode") %>' 
            DataSource='<%# GetAllDepartments() %>' 
            DataTextField="DeptCode" 
            DataValueField="DeptCode" />
    </EditItemTemplate>
</asp:TemplateField> 

当我在一行上单击编辑时,这非常有用,它会使用所有值填充 DropDownList 并为该行选择正确的值。

但是,当我尝试更新该行时:OnRowUpdating="UpdateRow"

protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = UserGV.Rows[e.RowIndex];
    DropDownList ddl = row.FindControl("ddlDeptCode") as DropDownList;
    string deptCode = ddl.SelectedValue;
}

它找到 DropDownList 控件,但 SelectedValue 始终为空字符串。
我需要访问所选值以保存到数据库。

关于如何在后面的代码中获取 Gridview 中 DropDownList 的 SelectedValue 的任何想法?

编辑:
您还可以从后面的代码中填充 DropDownList 和 SelectedValue:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
           var deptMgr = new DepartmentMgr();
           List<Department> departments = deptMgr.GetAllDepartments();
           DropDownList ddList = (DropDownList)e.Row.FindControl("ddlDeptCode");
           ddList.DataSource = departments;
           ddList.DataTextField = "DeptCode";
           ddList.DataValueField = "DeptCode";
           ddList.DataBind();

           string userDeptCode = DataBinder.Eval(e.Row.DataItem, "DeptCode").ToString();
           ddList.SelectedItem.Text = userDeptCode;
           ddList.SelectedValue = userDeptCode;
       }
    }
}
4

2 回答 2

2

绑定gridview时,我使用了一些技巧来获取表标题的第二个标题:

GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell th = new TableHeaderCell();
th.HorizontalAlign = HorizontalAlign.Center;
th.ColumnSpan = UserGV.Columns.Count;
th.BackColor = Color.SteelBlue;
th.ForeColor = Color.White;
th.Font.Bold = true;
th.Text = "Manage Users";
row.Cells.Add(th);
InnerTable.Rows.AddAt(0, row);

我不完全理解这对获取 DropDownList 控件的 SelectedValue 有何影响,但我一评论它就开始工作了。

对于那些感兴趣的人,我使用不同的方法得到了第二个标题:

在 .aspx 文件中,我将其添加到 Gridview:

OnRowCreated="CreateRow"

在后面的代码中,我添加了以下方法:

protected void CreateRow(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {           
        GridView gridView = (GridView)sender;
        GridViewRow row = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Normal);
        TableCell th = new TableHeaderCell();
        th.HorizontalAlign = HorizontalAlign.Center;
        th.ColumnSpan = UserGV.Columns.Count;
        th.ForeColor = Color.White;
        th.BackColor = Color.SteelBlue;
        th.Font.Bold = true;
        th.Text = "Manage Users";
        row.Cells.Add(th);

        gridView.Controls[0].Controls.AddAt(0, row);
    }
}

现在一切正常。

于 2014-04-09T19:29:46.827 回答
1

在编辑模板中,selectedvalue从数据绑定中设置,然后您将获得正确的选定值而不是空值。

 SelectedValue='<%# Bind("DeptCode") %>'
于 2021-01-23T06:07:33.647 回答