【问题标题】:Getting value from database on drop down menu selection change在下拉菜单选择更改时从数据库中获取值
【发布时间】:2018-12-18 19:53:19
【问题描述】:

我正在调用一种方法来填充 asp.net 网络表单中的下拉菜单。该方法调用检索名称列表的存储过程。它还从数据库中检索成员 ID。所有这一切都很好,但是,我需要成员 ID 字段随着下拉菜单中的每个关联名称而更改。例如,我从下拉列表中选择名称 Barney Fife,它返回成员 ID '1'。问题是当我从下拉列表中选择不同的名称时 - 会员 ID 没有改变。这是我检索数据的 C# 方法:

        public void GetMemberName()
    {
        try
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("dbo.spGetMemberNames", con))

                {
                    con.Open();
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    { 
                        sda.Fill(ds); // ds is declared in the scope.
                        cmbNames.DataSource = ds;
                        cmbNames.DataTextField = "FormattedName";
                        cmbNames.DataValueField = "FormattedName";                    

                        cmbNames.DataBind();                            
                    }
                    using (SqlDataReader read = cmd.ExecuteReader())
                    {
                        while (read.Read())
                        {
                            lblMemberID.Text = (read["MemberID"].ToString()); // Places the member ID associated with the name in a label.
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.ToString();
        }
    }

这是我的存储过程:

CREATE PROCEDURE [dbo].[spGetMemberNames]

AS
BEGIN
        SELECT FirstName + ' ' + LastName [FormattedName],MemberID
        FROM tblMembers
        ORDER BY LastName
END

我是否需要调用一个完全不同的方法和存储过程来连接到数据库并根据 DropDown SelectedIndexChange 上的 FormattedName(来自我的存储过程)查找成员 ID?

这是我的 SelectedIndexChange 事件。

        protected void cmbNames_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblMemberID.Text = "";
        try
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("dbo.spGetMemberNames", con))

                {
                    con.Open();
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        sda.Fill(ds);
                        cmbSelectTable.DataSource = ds;
                        cmbSelectTable.DataTextField = "FormattedName";
                        cmbSelectTable.DataValueField = "MemberID";
                        cmbSelectTable.DataBind();
                    }
                    using (SqlDataReader read = cmd.ExecuteReader())
                    {
                        while (read.Read())
                        {
                            lblMemberID.Text = (read["MemberID"].ToString());
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblWarning.Text = ex.ToString();
        }
    }

【问题讨论】:

  • 当您发布表单时,下拉列表的值会被传递,而不是文本。您目前拥有:cmbNames.DataValueField = "FormattedName";。我相信您想使用 Member ID 而不是 FormattedName 作为下拉选项的值。
  • 实际上,MemberID 值将对用户隐藏在一个隐藏标签中,该标签将传递给另一个表单。但是,这确实给了我创建一个隐藏的下拉框的想法,该下拉框也仅包含成员 ID。不过,我希望有一个更简单的解决方案。
  • 这是正确的方法。用户看不到值字段,只看到文本字段。除非出于其他目的(也仍然不需要),否则不需要额外的隐藏标签。现在,当名称 ddl 中的选择发生更改时,您无法更改 ID。

标签: c# asp.net stored-procedures drop-down-menu


【解决方案1】:

这太容易了。这是解决方案:

protected void cmbNames_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = cmbNames.SelectedValue;
    lblMemberID.Text = value;
}

【讨论】:

    【解决方案2】:

    将下拉列表的 autopostback 属性设置为 true。

    【讨论】:

    • 我已经这样做了,但它仍然没有改变标签。
    • 与我们分享您的下拉列表标记。此外,下拉列表的 SelectedValue 属性包含所选项目的 MemberID。更改 cmbNames.DataValueField="MemberID" 。如果您希望用户选择她想要的项目,请将 readonly 更改为 false。
    • 我在原帖中添加了下拉列表更改事件。
    猜你喜欢
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2021-10-25
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多