gpt4 book ai didi

c# - Gridview EditItemTemplate DropDownList 获取 SelectedValue

转载 作者:行者123 更新时间:2023-12-02 19:31:13 35 4
gpt4 key购买 nike

在我的 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;
}
}
}

最佳答案

在绑定(bind) 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,但当我注释掉它后,它就开始工作了。

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

在 .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);
}
}

现在一切正常。

关于c# - Gridview EditItemTemplate DropDownList 获取 SelectedValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945922/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com