如何使用下拉列表项显示关联值(从列表中选择后)?

时间:2016-05-14 13:35:26

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

我在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)
);

2 个答案:

答案 0 :(得分:1)

根据AutoPostBack=True使用if (!IsPostBack)Page_Load进行修改,感谢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数据源,因为如果设置数据源,默认情况下下拉列表将重置以选择列表中的第一项。

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

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

答案 1 :(得分:1)

是的你可以,但如果没有,请使用数据表,我相信工作正常。如果你不能这样做,我会给予更正。