.split("|");
//為dListChild添加條目
document.getElementById("dListChild").options.add(new Option(thisArray[1].toString(),thisArray[0].toString()));
}
}
}
}
//發(fā)送數據,請注意順序和參數,參數一定為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 的摘要說(shuō)明。
/// </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)
{
// 在此處放置用戶(hù)代碼以初始化頁(yè)面
//if(!IsPostBack)
//{
BindDrop();//如果不是提交回來(lái)就綁定列表框
//}
}
protected void BindDrop()
{
//首先我想父dropdownlist也綁定數據庫,后面想沒(méi)必要
//if(!IsPostBack)
//{
//綁定父dListParent
// BindParent();
//}
//獲得傳遞過(guò)來(lái)的parent_id值,如果是第一次請求他為null
string str = Request.QueryString["parent_id"];
string str1 = dListParent.SelectedValue;;
Response.Write(str1);
//如果str加個(gè)字符串!=原來(lái)的字符串則說(shuō)明觸發(fā)過(guò)dListParent的onchange事件
if((str+"abc")!="abc")
{
//綁定 dListChild控件
BindChild(str);//把傳來(lái)的父DropDownList的value做為參數
}
else
BindParent(str1);
}
protected void BindParent(string str)
{
//如果是第一次請求或者是刷新這個(gè)頁(yè)面則根據dListParent的值來(lái)選擇
//把參數轉化成int
int i = Convert.ToInt32(str);
dListChild.Items.Clear();
dListChild.Items.Add(new ListItem("全部型號","0"));
//得到數據庫連接字符串
string connStr = ConfigurationSettings.AppSettings["ConnString"].ToString();
//初始化個(gè)conn對象
SqlConnection conn = new SqlConnection(connStr);
//數據庫語(yǔ)句
string commStr = string.Format("select type_value,type_text from phone_type where parent_id={0}",i);
//建立數據庫命令對象
SqlCommand comm = new SqlCommand(commStr,conn);
//打開(kāi)數據庫
conn.Open();
//執行命令
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;
//添加下面這話(huà)的意思是當點(diǎn)提交按鈕提交窗體的時(shí)候第二個(gè)dListChild的狀態(tài)能夠得到保存
dListChild.SelectedValue = Request.Form["dListChild"];
dr.Close();
conn.Close();
}
protected void BindChild(string str)
{
//通過(guò)js給包括dropdownlist任何控件添加的內容不會(huì )被保存狀態(tài)
//把參數轉化成int
int i = Convert.ToInt32(str);
//定義個(gè)字符串用保存從數據庫返回的數據
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()));
}
//把從數據庫得到的信息輸出到客戶(hù)端
Response.Write(result);
//輸出完成關(guān)閉Response,以免造成不必要的輸出
Response.Flush();
Response.Close();
dr.Close();
conn.Close();
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </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());
}
}
}
數據表:
主鍵id parent_id(int) type_value(int) type_text(varchar)
int遞增 父下拉框的value值 下拉框的value 下拉框的text