實(shí)現(xiàn)無(wú)刷新DropDownList聯(lián)動(dòng)效果
Posted on 2007-07-15 16:40 停留的風(fēng) 閱讀(335) 評(píng)論(0) 編輯 收藏 實(shí)現(xiàn)無(wú)刷新DropDownList聯(lián)動(dòng)效果
在做一個(gè)文章添加功能時(shí),想在選擇大類后,自動(dòng)將其所屬二級(jí)小類顯示出來(lái),使用DropDownList的SelectedIndexChanged事件可以很容易實(shí)現(xiàn),但每次選擇后頁(yè)面總要刷新一次,讓人感覺(jué)很不爽。為實(shí)現(xiàn)DropDownList無(wú)刷新二級(jí)聯(lián)動(dòng),這幾天在網(wǎng)上找了些資料,但都無(wú)法達(dá)到我想要的效果,經(jīng)過(guò)反復(fù)調(diào)試,現(xiàn)已基本實(shí)現(xiàn)了此功能,現(xiàn)將代碼附下。
一、數(shù)據(jù)庫(kù)設(shè)計(jì):
二、設(shè)計(jì)步驟:
1、首先,我們新建一個(gè)頁(yè)面DropTest.aspx,在其中放入兩個(gè)DropDownList控件:DropDownList1和DropDownList2,其完整代碼如下:
<%@ Page language="c#" Codebehind="DropTest.aspx.cs" AutoEventWireup="false" Inherits="studyWEB.DropTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script>
function load(ClassID){ //ClassID為接收傳遞的大類編號(hào)
var drp2 = document.getElementById("DropDownList2");
function RemoveAll(oElem) { //清除DropDownList2的所有項(xiàng)
var i = 0;
for (i = oElem.length; i >= 0; i--){
oElem.options.remove(i);
}
}
RemoveAll(drp2)
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "DropChild.aspx?ClassID="+ClassID, false); //調(diào)用讀取小類數(shù)據(jù)的頁(yè)面,將大類
// 編號(hào)值傳遞過(guò)去
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("http://CLASSNAME/Table/ClassName"); //讀取所有請(qǐng)求大類所屬小類的類名
items2 = oDoc.selectNodes("http://CLASSNAME/Table/ClassID"); //讀取所有請(qǐng)求大類所屬小類的編號(hào)
var itemsLength=items1.length;
for(i=0;i<itemsLength;i++) //將小類的類名和編號(hào)賦予DropDownList2
{
var newOption = document.createElement("OPTION");
newOption.text=items1[i].text;
newOption.value=items2[i].text;
drp2.options.add(newOption);
}
}
</script>
</HEAD>
<body MS_POSITIONING="flowLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
<asp:TextBox id="TH" runat="server" BorderStyle="None" ForeColor="White" BorderColor="White"></asp:TextBox>
<asp:Label id="Label1" runat="server"></asp:Label>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
該頁(yè)面的后臺(tái)文件(DropDownList1.aspx.cs)中Page_Load內(nèi)的代碼如下:
if(!this.IsPostBack)
{
SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where ClassLevel=1",con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "ClassName";
this.DropDownList1.DataValueField = "ClassID";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)"); //將ClassID作為參數(shù)傳遞給腳本函數(shù)load(ClassID),如果要傳遞的是ClassName,應(yīng)將value改為innerText,但如果大類為中文,則調(diào)用小類時(shí)出現(xiàn)無(wú)法顯示的問(wèn)題
// this.DropDownList2.Attributes.Add("onChange","javascript:document.Form1.TH.value=this.options[this.selectedIndex].value;"); //讀取DropDownList2的值,將其賦給一個(gè)TextBox控件TH,以獲取DropDownList2的值,為獲取DropDownList2的值,網(wǎng)上有人說(shuō)可通過(guò)使用隱藏的TextBox控件來(lái)獲取,我未能實(shí)現(xiàn),因?yàn)樵诳蛻舳穗[藏的TextBox控件也是不可用腳本來(lái)訪問(wèn)的,沒(méi)法給其賦值,我只能通過(guò)將其樣式、字體顏色設(shè)于背景相同來(lái)達(dá)到隱藏效果,這是一個(gè)很笨的方法,有誰(shuí)有好的方法,請(qǐng)幫我。
}
此頁(yè)面實(shí)現(xiàn)如下功能:首先從數(shù)據(jù)庫(kù)內(nèi)讀取所有類級(jí)別為1(即大類)的類名和類編號(hào),綁定到DropDownList1控件上;然后通過(guò)DropDownList1的Attributes屬性調(diào)用javascript函數(shù)load(ClassID);load()函數(shù)通過(guò)調(diào)用DropChild.aspx頁(yè)面,讀取XML流,得到大類所屬小類的ClassName和ClassID。
2、新建DropChild.aspx頁(yè)面文件,其中不插入任何控件和文本,只在其后臺(tái)文件(DropChild.aspx.cs)中的Page_Load中加入以下代碼:
if(this.Request["ClassID"]!=null)
{
int state = Convert.ToInt32(this.Request["ClassID"]);
SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where UpClassID='"+state+"'",con);
DataSet ds = new DataSet("CLASSNAME");
da.Fill(ds);
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
ds.WriteXml(writer);
writer.Flush();
Response.End();
writer.Close();
該方法得到用戶選擇的大類的編號(hào),通過(guò)查詢以后得到一個(gè)DataSet對(duì)象,使用該對(duì)象的WriteXML方法直接將內(nèi)容寫到Response.OutputStream里面然后傳遞到客戶端,客戶端的load方法通過(guò)result =oHttpReq.responseText;句話得到一個(gè)XML字符串,最后解析此串。
另外,測(cè)試獲取DropDownList2值,添加了TextBox控件TH,當(dāng)點(diǎn)擊Button時(shí),處理事件代碼如下:
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=TH.Text;
}
在做一個(gè)文章添加功能時(shí),想在選擇大類后,自動(dòng)將其所屬二級(jí)小類顯示出來(lái),使用DropDownList的SelectedIndexChanged事件可以很容易實(shí)現(xiàn),但每次選擇后頁(yè)面總要刷新一次,讓人感覺(jué)很不爽。為實(shí)現(xiàn)DropDownList無(wú)刷新二級(jí)聯(lián)動(dòng),這幾天在網(wǎng)上找了些資料,但都無(wú)法達(dá)到我想要的效果,經(jīng)過(guò)反復(fù)調(diào)試,現(xiàn)已基本實(shí)現(xiàn)了此功能,現(xiàn)將代碼附下。
一、數(shù)據(jù)庫(kù)設(shè)計(jì):
字段名 | 數(shù)據(jù)類型 | 說(shuō)明 |
ClassID | 自動(dòng)編號(hào) | 類編號(hào) |
ClassName | varchar(8) | 類名 |
UpClassID | int(4) | 上級(jí)類編號(hào) |
ClassLevel | int(4) | 類級(jí)別,1為大類,2為小類 |
二、設(shè)計(jì)步驟:
1、首先,我們新建一個(gè)頁(yè)面DropTest.aspx,在其中放入兩個(gè)DropDownList控件:DropDownList1和DropDownList2,其完整代碼如下:
<%@ Page language="c#" Codebehind="DropTest.aspx.cs" AutoEventWireup="false" Inherits="studyWEB.DropTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script>
function load(ClassID){ //ClassID為接收傳遞的大類編號(hào)
var drp2 = document.getElementById("DropDownList2");
function RemoveAll(oElem) { //清除DropDownList2的所有項(xiàng)
var i = 0;
for (i = oElem.length; i >= 0; i--){
oElem.options.remove(i);
}
}
RemoveAll(drp2)
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "DropChild.aspx?ClassID="+ClassID, false); //調(diào)用讀取小類數(shù)據(jù)的頁(yè)面,將大類
// 編號(hào)值傳遞過(guò)去
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("http://CLASSNAME/Table/ClassName"); //讀取所有請(qǐng)求大類所屬小類的類名
items2 = oDoc.selectNodes("http://CLASSNAME/Table/ClassID"); //讀取所有請(qǐng)求大類所屬小類的編號(hào)
var itemsLength=items1.length;
for(i=0;i<itemsLength;i++) //將小類的類名和編號(hào)賦予DropDownList2
{
var newOption = document.createElement("OPTION");
newOption.text=items1[i].text;
newOption.value=items2[i].text;
drp2.options.add(newOption);
}
}
</script>
</HEAD>
<body MS_POSITIONING="flowLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
<asp:TextBox id="TH" runat="server" BorderStyle="None" ForeColor="White" BorderColor="White"></asp:TextBox>
<asp:Label id="Label1" runat="server"></asp:Label>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
該頁(yè)面的后臺(tái)文件(DropDownList1.aspx.cs)中Page_Load內(nèi)的代碼如下:
if(!this.IsPostBack)
{
SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where ClassLevel=1",con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "ClassName";
this.DropDownList1.DataValueField = "ClassID";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)"); //將ClassID作為參數(shù)傳遞給腳本函數(shù)load(ClassID),如果要傳遞的是ClassName,應(yīng)將value改為innerText,但如果大類為中文,則調(diào)用小類時(shí)出現(xiàn)無(wú)法顯示的問(wèn)題
// this.DropDownList2.Attributes.Add("onChange","javascript:document.Form1.TH.value=this.options[this.selectedIndex].value;"); //讀取DropDownList2的值,將其賦給一個(gè)TextBox控件TH,以獲取DropDownList2的值,為獲取DropDownList2的值,網(wǎng)上有人說(shuō)可通過(guò)使用隱藏的TextBox控件來(lái)獲取,我未能實(shí)現(xiàn),因?yàn)樵诳蛻舳穗[藏的TextBox控件也是不可用腳本來(lái)訪問(wèn)的,沒(méi)法給其賦值,我只能通過(guò)將其樣式、字體顏色設(shè)于背景相同來(lái)達(dá)到隱藏效果,這是一個(gè)很笨的方法,有誰(shuí)有好的方法,請(qǐng)幫我。
}
此頁(yè)面實(shí)現(xiàn)如下功能:首先從數(shù)據(jù)庫(kù)內(nèi)讀取所有類級(jí)別為1(即大類)的類名和類編號(hào),綁定到DropDownList1控件上;然后通過(guò)DropDownList1的Attributes屬性調(diào)用javascript函數(shù)load(ClassID);load()函數(shù)通過(guò)調(diào)用DropChild.aspx頁(yè)面,讀取XML流,得到大類所屬小類的ClassName和ClassID。
2、新建DropChild.aspx頁(yè)面文件,其中不插入任何控件和文本,只在其后臺(tái)文件(DropChild.aspx.cs)中的Page_Load中加入以下代碼:
if(this.Request["ClassID"]!=null)
{
int state = Convert.ToInt32(this.Request["ClassID"]);
SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where UpClassID='"+state+"'",con);
DataSet ds = new DataSet("CLASSNAME");
da.Fill(ds);
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
ds.WriteXml(writer);
writer.Flush();
Response.End();
writer.Close();
該方法得到用戶選擇的大類的編號(hào),通過(guò)查詢以后得到一個(gè)DataSet對(duì)象,使用該對(duì)象的WriteXML方法直接將內(nèi)容寫到Response.OutputStream里面然后傳遞到客戶端,客戶端的load方法通過(guò)result =oHttpReq.responseText;句話得到一個(gè)XML字符串,最后解析此串。
另外,測(cè)試獲取DropDownList2值,添加了TextBox控件TH,當(dāng)點(diǎn)擊Button時(shí),處理事件代碼如下:
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=TH.Text;
}