.split("|");
//為dListChild添加條目
document.getElementById("dListChild").options.add(new Option(thisArray[1].toString(),thisArray[0].toString()));
}
}
}
}
//發(fā)送數(shù)據(jù),請注意順序和參數(shù),參數(shù)一定為null或者""
xmlhttp.send(null);
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList onchange="Go(this)" id="dListParent" style="Z-INDEX: 101; LEFT: 248px; POSITION: absolute; TOP: 40px"
runat="server">
<asp:ListItem Value="100">摩托羅拉</asp:ListItem>
<asp:ListItem Value="101">諾基亞</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="dListChild" style="Z-INDEX: 102; LEFT: 248px; POSITION: absolute; TOP: 104px"
runat="server"></asp:DropDownList>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 256px; POSITION: absolute; TOP: 176px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>后臺webform1.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
namespace drop
{
/// <summary>
/// WebForm1 的摘要說明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList dListParent;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DropDownList dListChild;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
//if(!IsPostBack)
//{
BindDrop();//如果不是提交回來就綁定列表框
//}
}
protected void BindDrop()
{
//首先我想父dropdownlist也綁定數(shù)據(jù)庫,后面想沒必要
//if(!IsPostBack)
//{
//綁定父dListParent
// BindParent();
//}
//獲得傳遞過來的parent_id值,如果是第一次請求他為null
string str = Request.QueryString["parent_id"];
string str1 = dListParent.SelectedValue;;
Response.Write(str1);
//如果str加個字符串!=原來的字符串則說明觸發(fā)過dListParent的onchange事件
if((str+"abc")!="abc")
{
//綁定 dListChild控件
BindChild(str);//把傳來的父DropDownList的value做為參數(shù)
}
else
BindParent(str1);
}
protected void BindParent(string str)
{
//如果是第一次請求或者是刷新這個頁面則根據(jù)dListParent的值來選擇
//把參數(shù)轉(zhuǎn)化成int
int i = Convert.ToInt32(str);
dListChild.Items.Clear();
dListChild.Items.Add(new ListItem("全部型號","0"));
//得到數(shù)據(jù)庫連接字符串
string connStr = ConfigurationSettings.AppSettings["ConnString"].ToString();
//初始化個conn對象
SqlConnection conn = new SqlConnection(connStr);
//數(shù)據(jù)庫語句
string commStr = string.Format("select type_value,type_text from phone_type where parent_id={0}",i);
//建立數(shù)據(jù)庫命令對象
SqlCommand comm = new SqlCommand(commStr,conn);
//打開數(shù)據(jù)庫
conn.Open();
//執(zhí)行命令
SqlDataReader dr = comm.ExecuteReader();
//循環(huán)dr,給dListParent添加條目
while(dr.Read())
{
dListChild.Items.Add(new ListItem(dr[1].ToString(),dr[0].ToString()));
//也可以這樣
//dListParent.Items.Add(new ListItem(dr["phone_text"].ToString(),dr["phone_value"].ToString()));
}
dListParent.Items[0].Selected = true;
//添加下面這話的意思是當(dāng)點(diǎn)提交按鈕提交窗體的時候第二個dListChild的狀態(tài)能夠得到保存
dListChild.SelectedValue = Request.Form["dListChild"];
dr.Close();
conn.Close();
}
protected void BindChild(string str)
{
//通過js給包括dropdownlist任何控件添加的內(nèi)容不會被保存狀態(tài)
//把參數(shù)轉(zhuǎn)化成int
int i = Convert.ToInt32(str);
//定義個字符串用保存從數(shù)據(jù)庫返回的數(shù)據(jù)
string result = "";
//先清空輸出的東西
Response.Clear();
string connStr = ConfigurationSettings.AppSettings["ConnString"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand comm = conn.CreateCommand();
string commStr = string.Format("select type_value,type_text from phone_type where parent_id = {0}",i);
comm.CommandText = commStr;
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
while(dr.Read())
{
result += ","+dr[0].ToString() +"|" + dr[1].ToString();
//dListChild.Items.Add(new ListItem(dr[1].ToString(),dr[0].ToString()));
}
//把從數(shù)據(jù)庫得到的信息輸出到客戶端
Response.Write(result);
//輸出完成關(guān)閉Response,以免造成不必要的輸出
Response.Flush();
Response.Close();
dr.Close();
conn.Close();
}
#region Web 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write(Request.Form["dListChild"].ToString());
}
}
}
數(shù)據(jù)表:
主鍵id parent_id(int) type_value(int) type_text(varchar)
int遞增 父下拉框的value值 下拉框的value 下拉框的text