【问题标题】:How do I show an associated value with a dropdown list item (after choosing it from the list)?如何显示与下拉列表项的关联值(从列表中选择后)?
【发布时间】:2016-09-10 16:09:46
【问题描述】:

我在 ASP.NET,Visual Studio 中编程。我有一个以 HTML 形式创建的下拉列表。如果我下拉列表,它会显示表中关联列的记录。但我想要的是显示与该列表项对应的值/记录。

例如在表格中,我有列idproductnameprice。选择特定产品名称(从下拉列表中)后,与之相关的价格必须显示在其前面(在标签中)。

但是,默认情况下,我希望下拉列表在开头不显示任何内容。

更新:

Store.aspx:

<form id="form1" runat="server">
    <div>
        Welcome
        <asp:Label ID="Label3" runat="server" ></asp:Label>
        <br />
        <br />
       Products: <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" ></asp:DropDownList>
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [productdata]"></asp:SqlDataSource>

    &nbsp;Price:
        <asp:Label ID="Label1" runat="server" ></asp:Label>
        <br />
        <br />
         <asp:Button ID="Button1" runat="server" Text="Add to Cart" />
        <br />
        <br />
        Items in cart: <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>

        <br />
        <br />

        Total Price: <asp:Label ID="Label2" runat="server"></asp:Label>

    </div>
    </form>

Store.aspx.cs:

    public partial class Store : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label3.Text = Request.QueryString["name"];//show welcome text
            String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            if (!IsPostBack)
            {
                using (SqlConnection sc = new SqlConnection(cs))
                {
                    SqlCommand sqlcom = new SqlCommand("Select id, productname, price from productdata", sc);
                    sc.Open();
                    DropDownList1.DataTextField = "productname";//show in the dropdown list
                    DropDownList1.DataValueField = "price"; //show in the label
                    DropDownList1.DataSource = sqlcom.ExecuteReader();
                    DropDownList1.DataBind();
                }
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlDataReader rd;
            using (SqlConnection sc = new SqlConnection(cs))
            {
                SqlCommand sqlcom = new SqlCommand("Select id, productname, price from productdata where id=" + Convert.ToUInt32(DropDownList1.SelectedValue), sc);
                sc.Open();

                rd = sqlcom.ExecuteReader();
                if (rd.Read())
                {
                    Label1.Text = rd[2].ToString();
                }
                sc.Close();

            }

        }
 }

数据库:

CREATE TABLE [dbo].[productdata] (
    [Id]          INT          NOT NULL,
    [productname] VARCHAR (50) NULL,
    [price]       FLOAT (53)   NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

【问题讨论】:

  • 你好像用的是sql server,但是你标记了mysql,请更新一下。

标签: c# html asp.net sql-server visual-studio


【解决方案1】:

此编辑根据在Page_Load 中使用AutoPostBack=Trueif (!IsPostBack) 感谢Arindam

对于使用回发事件的简单解决方案:

首先你应该为下拉列表添加OnSelectedIndexChanged事件

<asp:DropDownList ID="DropDownList1" runat="server" 
     OnSelectedIndexChanged="GetPrice" AutoPostBack="true">
</asp:DropDownList>

然后在后面的代码中,您只需获取选定的值并填写标签价格

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Label3.Text = Request.QueryString["name"];//show welcome text
        String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection sc = new SqlConnection(cs))
        {
            SqlCommand sqlcom = new SqlCommand("Select id, productname, price from productdata", sc);
            sc.Open();
            DropDownList1.DataTextField = "productname";//show in the dropdown list
            DropDownList1.DataValueField = "price"; //show in the label
            DropDownList1.DataSource = sqlcom.ExecuteReader();
            DropDownList1.DataBind();
        }
    }
}
protected void GetPrice(object sender, EventArgs e)
{
    Label1.Text = DropDownList1.SelectedValue;
}
  1. 您必须使用AutoPostBack=True,这样当您更改下拉列表的索引时,它将触发回发到服务器,因此将调用函数GetPrice(...)

  2. 每次页面postback,都会先调用函数Page_Load(...),所以必须使用属性IsPostBack来检查case1_这是不是第一次页面加载,或 case2_a 回发事件,并且您只在 case1 设置 ddl 数据源,因为如果您设置数据源,默认情况下下拉列表将重置选择列表中的第一项。

当你前进的时候,你应该考虑使用Javascript和Jquery来解决这个问题,这样页面就不会像这个回发解决方案那样再次加载了。

还有一件事,你应该很好地命名你的控件,不要让它们像那样默认。这是two hard things in programming 之一。

【讨论】:

  • Pham X. Bach,谢谢,但不幸的是它什么也没做。请看我的更新。
  • 您是否将函数protected void GetPrice(..... 放入您的Store.aspx.cs 代码中?
  • Pham X. Bach,是的,但是在我构建项目之后,错误就消失了。但它没有做它应该做的事情。我要更新代码给你看,。
【解决方案2】:

是的,你可以,但如果不能,请使用数据表,我相信它可以正常工作。如果你不能做到这一点,我会给出更正。

【讨论】:

  • Arindam ,数据表?好的,我已经用我现在所拥有的内容更新了帖子。
  • 好的,如果你的意思是数据库表,我有。我正在从数据库表中获取数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多