如何通过选择类别通过下拉列表搜索请求的页面?

时间:2013-11-21 09:08:43

标签: asp.net sql-server

我的应用程序中有四个下拉列表和一个按钮。 1'st下拉列表包含电视,汽车,笔记本电脑,手机等产品详细信息。 2'nd下拉列表包含三星,戴尔,宝马,诺基亚等产品公司的详细信息。 3'rd下拉包含诺基亚1110,宝马x5,戴尔Vostro 1550等产品型号。 第4个下拉列表包含海得拉巴,孟买,加尔各答等城市名称。

如果用户选择第一个下拉列表作为汽车,则可以在第二个下拉列表中打开可用的汽车公司,如果用户将第二个下拉列表设置为Bmw,则第三个下拉列表应显示Bmw中可用的模型。如果用户选择海德拉巴作为城市,则第四次下拉列表应显示印度的城市。应打开有关海得拉巴页面中Bmwx5的详细信息。

2 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题,答案就是你有四个下拉列表

ids>> A,B,C,d

现在


第一次下拉

 <asp:DropDownList OnSelectedIndexChanged="ddl_select_changed1" ID="a" runat="server">
    now in your code file 
    protected void ddl_select_changed1(object sender, EventArgs e)
    {
//change b,c,d dropdowns
     }

现在另一个

第二次下拉

 <asp:DropDownList OnSelectedIndexChanged="ddl_select_changed2" ID="b" runat="server">
    now in your code file 
    protected void ddl_select_changed2(object sender, EventArgs e)
    {
//change c,d dropdowns
     }

现在另一个


第3次下拉

<asp:DropDownList OnSelectedIndexChanged="ddl_select_changed3" ID="b" runat="server">
    now in your code file 
    protected void ddl_select_changed3(object sender, EventArgs e)
    {
//change d dropdowns
     }

这是你可以达到你的要求的方式我希望这会对你有所帮助......问候......:)

答案 1 :(得分:0)

我没有测试过,但确定这就是你要找的东西:

代码背后的变化:

protected void Page_Load(object sender, EventArgs e)
{
    //Bind data to first drop down box on pageload
    DropDownList1.DataTextField = "ItemName";//field returned from db
    DropDownList1.DataValueField = "ItemID";//field returned from db
    //binds to data returned from db - first items have no parent id's
    DropDownList1.DataSource = GetItems(0);
    DropDownList1.DataBind();
    DropDownList1.Items.Insert(0, new ListItem("All Borgs", Session["BorgID"].ToString()));
    DropDownList1.Items[0].Selected = true;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //When value is selected in first drop down populate second drop down using the id selected in first drop down
    DropDownList2.DataTextField = "ItemName";//field returned from db
    DropDownList2.DataValueField = "ItemID";//field returned from db
    //binds to data returned from db
    DropDownList2.DataSource = GetItems(Convert.ToInt32(DropDownList1.DataValueField));
    DropDownList2.DataBind();
    DropDownList2.Items.Insert(0, new ListItem("[Choose]", "-1"));
    DropDownList2.Items[0].Selected = true;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    //When value is selected in second drop down populate third drop down using the id selected in second drop down
    DropDownList3.DataTextField = "ItemName";//field returned from db
    DropDownList3.DataValueField = "ItemID";//field returned from db
    //binds to data returned from db
    DropDownList3.DataSource = GetItems(Convert.ToInt32(DropDownList2.DataValueField));
    DropDownList3.DataBind();
    DropDownList3.Items.Insert(0, new ListItem("[Choose]", "-1"));
    DropDownList3.Items[0].Selected = true;
}

public DataTable GetItems(int _ParentItemID)
{
    SqlConnection sqlconn = null;

    sqlconn = new SqlConnection();
    //SQLConnectionstring declared in web.config to connect to db
    sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["SQLConnectionstring"].ConnectionString;

    //select data from database
    SqlCommand sqlcommand = new SqlCommand("SELECT ItemName, ItemID  FROM YourTable WHERE ParentItemID = " + _ParentItemID, sqlconn);

    sqlcommand.CommandType = System.Data.CommandType.Text;
    SqlDataAdapter adapter = new SqlDataAdapter(sqlcommand);
    //create and fill datatable to return results to call and bind
    DataTable dt = new DataTable();
    adapter.Fill(dt);

    sqlconn.Dispose();
    sqlconn.Close();

    return dt;
}

前端改变:

<asp:DropDownList ID="DropDownList1" runat="server" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />

<asp:DropDownList ID="DropDownList2" runat="server" 
    onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<br />

<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>