DropDownList无刷新易做图联动
创建一个项目,建一个*.aspx面页,切换到html下。输入以下代码:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript">
<!--
function XmlPost(str)
{
var webFileUrl="";
document.all("<% =Lbx_ClassThree.ClientID %>").length=0;
if(str==1)
{
webFileUrl = "?oneid=" + document.all("<% =Lbx_ClassOne.ClientID %>").value;
document.all("<% =Lbx_ClassTwo.ClientID %>").length=0;
}
else
{
webFileUrl = "?oneid=" + document.all("<% =Lbx_ClassOne.ClientID %>").value+"&twoid="+document.all("<% =Lbx_ClassTwo.ClientID %>").value;
}
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("Post", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "")
{
var piArray = result.split(",");
if(str==1)
{
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
document.all("<% =Lbx_ClassTwo.ClientID %>").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
{
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
document.all("<% =Lbx_ClassThree.ClientID %>").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
}
}
//-->
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DropDownList id="Lbx_ClassOne" runat="server" Width="100"></asp:DropDownList>
<asp:DropDownList id="Lbx_ClassTwo" runat="server" Width="100"></asp:DropDownList>
<asp:DropDownList id="Lbx_ClassThree" runat="server" Width="100"></asp:DropDownList></FONT>
</form>
</body>
</HTML>
然后切换到*.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.Data.OleDb;
namespace jilian
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Lbx_ClassOne;
protected System.Web.UI.WebControls.DropDownList Lbx_ClassTwo;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.DropDownList Lbx_ClassThree;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strOneid = "", strTwoid = "";
if (Request["oneid"] != null && Request["oneid"].ToString() != "")
{
strOneid = Request["oneid"].ToString();
}
if (Request["twoid"] != null && Request["twoid"].ToString() != "")
{
strTwoid = Request["twoid"].ToString();
}
if (strOneid != "")
{
Lbx_Class_Bind(strOneid, strTwoid);
}
if (!this.IsPostBack)
{
Lbx_ClassOne_Bind();
Lbx_ClassOne.Attributes.Add("onchange", "XmlPost(1)");
Lbx_ClassTwo.Attributes.Add("onchange", "XmlPost(2)");
}
}
private void Lbx_ClassOne_Bind()
{
string strSQL;
strSQL = "select * from nts_infoclass where oneid<>0 and twoid=0 and threeid=0 order by sort";
string ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;";
ConnectionString+=@"Data Source=D:\class\database.mdb";
OleDbConnection cnn = new OleDbConnection(ConnectionString);
cnn.Open();
OleDbCommand cmd=new OleDbCommand(strSQL, cnn);
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Lbx_ClassOne.DataSource = dr;
Lbx_ClassOne.DataTextField = "classname";
Lbx_ClassOne.DataValueField = "oneid";
Lbx_ClassOne.DataBind();
}
private void Lbx_Class_Bind(string oneid, string twoid)
{
string strSQL = "",idname="";
if (oneid != "" && twoid == "")
{
strSQL = "select * from nts_infoclass where twoid<>0 and threeid=0 and oneid=" + oneid + " order by sort";
idname = "twoid";
}
if (oneid != "" && twoid != "")
{
strSQL = "select * from nts_infoclass where threeid<>0 and oneid=" + oneid + " and twoid=" + twoid + " order by sort";
idname = "threeid";
}
string mystr = "";
//string ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='&D:\class\database.mdb'";
string ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;";
ConnectionString+=@"Data Source=D:\class\database.mdb";
OleDbConnection cnn = new OleDbConnection(ConnectionString);
cnn.Open();
OleDbCommand cmd=new OleDbCommand(strSQL, cnn);
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
mystr += "," + dr[idname].ToString() + "|" + dr["classname"].ToString();
}
if (mystr != "")
{
mystr = mystr.Substring(1);
}
dr.Close();
this.Response.Write(mystr);
this.Response.End();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
就ok啦,数据库我把它上传到我的资源里面了,是免费的,快来吧,"记主,数据库名了秋标签名相同,快来下载吧" --------------------编程问答-------------------- asp.net ajax群 技术讨论 1927901 欢迎参加
补充:.NET技术 , ASP.NET