如何从代码隐藏的 ASP.NET Web 窗体 C# 应用程序中的下拉列表中绑定 'SelectedValue'?
How to bind the 'SelectedValue' in dropdown in ASP.NET Web Forms C# application from code behind?
我正在尝试在 FormView 的 EditItemTemplate 中使用下拉列表。我正在从数据表中填充下拉列表。下拉列表按预期填充,但当我尝试保存更改时,下拉字段为空。在 FormView 中进行更改时如何保存所选值?
通常我只会将 SelectedValue='<%# Bind("Surname") %>' 放在下拉标记中,但显然我不能,因为我在后面的代码中填充了它。
这是下拉标记:
<asp:DropDownList runat="server" ID="SurnameDdl" AppendDataBoundItems="True" ></asp:DropDownList>
这是我用来填充下拉列表的数据表代码:
private DataTable GetAdUsers()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6]
{
new DataColumn("givenName", typeof (string)),
new DataColumn("sn", typeof (string)),
new DataColumn("mail", typeof (string)),
new DataColumn("department", typeof (string)),
new DataColumn("manager", typeof (string)),
new DataColumn("DisplayField", System.Type.GetType("System.String"), "sn + ' ,' + givenName + ' ' + mail")
});
using (var context = new PrincipalContext(ContextType.Domain, null))
{
using (var group = (GroupPrincipal.FindByIdentity(context, "TWIA Underwriting"))) //Options include Users, Everybody, Agent Services, UnderwritingAdmin
{
var users = group.GetMembers(true);
foreach (UserPrincipal user in users)
{
DirectoryEntry de = user.GetUnderlyingObject() as DirectoryEntry;
dt.Rows.Add
(
Convert.ToString(de.Properties["givenName"].Value),
Convert.ToString(de.Properties["sn"].Value),
Convert.ToString(de.Properties["mail"].Value),
Convert.ToString(de.Properties["department"].Value),
Regex.Replace((Convert.ToString(de.Properties["manager"].Value)), @"CN=([^,]*),.*$", ""),
de.Properties["DisplayField"].Value
);
}
}
}
return dt;
}
这是 FormView 的 OnDataBound 事件:
protected void fvPhaudDets_OnDataBound(object sender, EventArgs e)
{
if (fvPhaudDets.CurrentMode == FormViewMode.Edit)
{
DropDownList ddl = null;
ddl = (DropDownList)fvPhaudDets.FindControl("SurnameDdl");
ddl.DataSource = GetAdUsers();
ddl.DataTextField = "DisplayField";
ddl.DataValueField = "sn";
ddl.DataBind();
ddl.SelectedValue = "Surname";
}
}
我还需要向下拉列表中添加事件和代码吗?如果可以,需要什么?
您可以在 FormView 上使用 FindControl
来定位 DropDownList。它也适用于编辑模式。如果你有这样的 FormView
<asp:FormView ID="FormView1" runat="server">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Aa</asp:ListItem>
<asp:ListItem>Bb</asp:ListItem>
<asp:ListItem>Cc</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:FormView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
然后绑定数据,设置编辑模式进行测试
if (IsPostBack == false)
{
FormView1.DataSource = new string[1];
FormView1.ChangeMode(FormViewMode.Edit);
FormView1.DataBind();
}
您现在可以获取按钮点击的值
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList ddl = FormView1.FindControl("DropDownList1") as DropDownList;
Label1.Text = ddl.SelectedValue;
}
我正在尝试在 FormView 的 EditItemTemplate 中使用下拉列表。我正在从数据表中填充下拉列表。下拉列表按预期填充,但当我尝试保存更改时,下拉字段为空。在 FormView 中进行更改时如何保存所选值?
通常我只会将 SelectedValue='<%# Bind("Surname") %>' 放在下拉标记中,但显然我不能,因为我在后面的代码中填充了它。
这是下拉标记:
<asp:DropDownList runat="server" ID="SurnameDdl" AppendDataBoundItems="True" ></asp:DropDownList>
这是我用来填充下拉列表的数据表代码:
private DataTable GetAdUsers()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6]
{
new DataColumn("givenName", typeof (string)),
new DataColumn("sn", typeof (string)),
new DataColumn("mail", typeof (string)),
new DataColumn("department", typeof (string)),
new DataColumn("manager", typeof (string)),
new DataColumn("DisplayField", System.Type.GetType("System.String"), "sn + ' ,' + givenName + ' ' + mail")
});
using (var context = new PrincipalContext(ContextType.Domain, null))
{
using (var group = (GroupPrincipal.FindByIdentity(context, "TWIA Underwriting"))) //Options include Users, Everybody, Agent Services, UnderwritingAdmin
{
var users = group.GetMembers(true);
foreach (UserPrincipal user in users)
{
DirectoryEntry de = user.GetUnderlyingObject() as DirectoryEntry;
dt.Rows.Add
(
Convert.ToString(de.Properties["givenName"].Value),
Convert.ToString(de.Properties["sn"].Value),
Convert.ToString(de.Properties["mail"].Value),
Convert.ToString(de.Properties["department"].Value),
Regex.Replace((Convert.ToString(de.Properties["manager"].Value)), @"CN=([^,]*),.*$", ""),
de.Properties["DisplayField"].Value
);
}
}
}
return dt;
}
这是 FormView 的 OnDataBound 事件:
protected void fvPhaudDets_OnDataBound(object sender, EventArgs e)
{
if (fvPhaudDets.CurrentMode == FormViewMode.Edit)
{
DropDownList ddl = null;
ddl = (DropDownList)fvPhaudDets.FindControl("SurnameDdl");
ddl.DataSource = GetAdUsers();
ddl.DataTextField = "DisplayField";
ddl.DataValueField = "sn";
ddl.DataBind();
ddl.SelectedValue = "Surname";
}
}
我还需要向下拉列表中添加事件和代码吗?如果可以,需要什么?
您可以在 FormView 上使用 FindControl
来定位 DropDownList。它也适用于编辑模式。如果你有这样的 FormView
<asp:FormView ID="FormView1" runat="server">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Aa</asp:ListItem>
<asp:ListItem>Bb</asp:ListItem>
<asp:ListItem>Cc</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:FormView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
然后绑定数据,设置编辑模式进行测试
if (IsPostBack == false)
{
FormView1.DataSource = new string[1];
FormView1.ChangeMode(FormViewMode.Edit);
FormView1.DataBind();
}
您现在可以获取按钮点击的值
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList ddl = FormView1.FindControl("DropDownList1") as DropDownList;
Label1.Text = ddl.SelectedValue;
}