ASP.Net - 如何使用单个数据创建级联 DropDownLists 框 table
ASP.Net - How to Create Cascading DropDownLists Boxes Using Single Data table
我想从单个数据 table 创建 cascading/dependent 下拉框。
所有数据都在单个 table 中,例如国家、州、城市。
当用户 select 下拉列表框中的国家/地区状态应自动填写等等...
有许多使用不同 table 的示例,但如何使用单个 table 来做到这一点。请help/suggest/anylink.....
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 中。
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>
我想从单个数据 table 创建 cascading/dependent 下拉框。 所有数据都在单个 table 中,例如国家、州、城市。
当用户 select 下拉列表框中的国家/地区状态应自动填写等等...
有许多使用不同 table 的示例,但如何使用单个 table 来做到这一点。请help/suggest/anylink.....
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 中。
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>