本文介绍了ASP.Net-如何使用单个数据表创建级联的DropDownLists框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从单个数据表创建级联/依赖的下拉框.所有数据都在单个表格中,例如国家/地区,州/城市.

I want to create cascading/dependent DropDown boxes from single data table.All data is in single table such as country,state,city.

当用户从下拉列表框中选择国家/地区时,状态应自动填充,依此类推...

When user select country from DropDown list box states should be filled automatically and so on...

有很多使用不同表的示例,但是如何使用单个表来完成.请帮助/建议/任何链接....

There are many example which are using different table but how to do it using single table. please help/suggest/any link....

    string str5 = "SELECT distinct States FROM tblMain
    da1 = new SqlDataAdapter(str5, con);
    dt1 = new DataTable();
    da1.Fill(dt1);
    ddlEmpName.DataSource = dt1;
    ddlEmpName.DataTextField = "States";
    ddlEmpName.DataValueField = "States";
    ddlEmpName.DataBind();

推荐答案

也许这会让您入门.所有数据都在单个DataTable中,并将显示在同一DropDownList中.

Maybe this will get you started. All the data is in a single DataTable and will be displayed in the same DropDownList.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //create a datatable with three columns
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("Name", typeof(string));

            //add nine datarows
            dt.Rows.Add(0, "Country", "Netherlands");
            dt.Rows.Add(1, "Country", "Japan");
            dt.Rows.Add(2, "Country", "America");
            dt.Rows.Add(3, "State", "Gelderland");
            dt.Rows.Add(4, "State", "Texas");
            dt.Rows.Add(5, "State", "Echizen");
            dt.Rows.Add(6, "City", "Amsterdam");
            dt.Rows.Add(7, "City", "Tokyo");
            dt.Rows.Add(8, "City", "New York");

            //save the table in a viewstate
            ViewState["myViewState"] = dt;

            //fill the dropdown
            fillDropDownList("Country");
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //split the SelectedValue to find the next type of data for the dropdown
        string[] valueArr = DropDownList1.SelectedValue.Split('_');
        string filterType = valueArr[1];

        //get the next type
        if (filterType == "Country")
        {
            filterType = "State";
        }
        else if (filterType == "State")
        {
            filterType = "City";
        }
        else
        {
            filterType = "Country";
        }

        Label1.Text += valueArr[0] + "<br>";

        //refill the dropdown
        fillDropDownList(filterType);
    }

    private void fillDropDownList(string filterType)
    {
        //get the viewstate and cast it as a datatable
        DataTable dt = ViewState["myViewState"] as DataTable;

        //filter the correct rows with Linq
        DataTable dtFiltered = dt.AsEnumerable().Where(row => row.Field<string>("Type") == filterType).CopyToDataTable();

        //clear the existing items in the dropdown
        DropDownList1.Items.Clear();

        //loop the filtered items and add them to the dropdown
        for (int i = 0; i < dtFiltered.Rows.Count; i++)
        {
            string text = dtFiltered.Rows[i][2].ToString();
            //make the value a combination of the name and type to split in SelectedIndexChanged
            string value = text + "_" + dtFiltered.Rows[i][1].ToString();

            DropDownList1.Items.Insert(i, new ListItem(text, value, true));
        }
        DropDownList1.Items.Insert(0, new ListItem("Select...", "", true));
    }

在aspx页面中:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<br /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>

这篇关于ASP.Net-如何使用单个数据表创建级联的DropDownLists框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 13:01