【问题标题】:How to bind the 'SelectedValue' in dropdown in ASP.NET Web Forms C# application from code behind?如何从后面的代码绑定 ASP.NET Web Forms C# 应用程序的下拉列表中的“SelectedValue”?
【发布时间】:2018-09-24 21:12:25
【问题描述】:

我正在尝试在 FormView 的 EditItemTemplate 中使用下拉列表。我正在从数据表中填充下拉列表。下拉列表按预期填充,但是当我尝试保存更改时,下拉字段为空。在 FormView 中进行更改时如何保存选定的值?

通常我只会将 SelectedValue='' 放在下拉标记中,但显然我不能,因为我在后面的代码中填充它。

这是下拉标记:

<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=([^,]*),.*$", "$1"),
                    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";
    }
}

我还需要向下拉列表中添加事件和代码吗?如果需要,需要什么?

【问题讨论】:

    标签: c# asp.net formview


    【解决方案1】:

    您可以在 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多