DropDownList无刷新二级联动
一、数据表CAT结构:
二、配置web.config:
在<system.web>部分加入:
Ajax.dll下载
CREATE TABLE [dbo].[CAT] (
[CAT_ID] [int] IDENTITY (1, 1) NOT NULL ,
[CAT_NAME] [nvarchar] (20) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[PARENT_ID] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CAT] ADD
CONSTRAINT [DF_CAT_PARENT_ID] DEFAULT (0) FOR [PARENT_ID],
CONSTRAINT [PK_CAT] PRIMARY KEY CLUSTERED
(
[CAT_ID]
) ON [PRIMARY]
GO
[CAT_ID] [int] IDENTITY (1, 1) NOT NULL ,
[CAT_NAME] [nvarchar] (20) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[PARENT_ID] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CAT] ADD
CONSTRAINT [DF_CAT_PARENT_ID] DEFAULT (0) FOR [PARENT_ID],
CONSTRAINT [PK_CAT] PRIMARY KEY CLUSTERED
(
[CAT_ID]
) ON [PRIMARY]
GO
二、配置web.config:
在<system.web>部分加入:
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
三、添加AjaxMethod.cs:<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// AjaxMethod 的摘要描述
/// </summary>
public class AjaxMethod
{
GetDataSet
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static DataSet GetCatList(string id)
{
string sql = "SELECT CAT_ID,CAT_NAME FROM CAT WHERE PARENT_ID='" + id.ToString() + "'";
return GetDataSet(sql);
}
}
四、Dropdownlist.aspx代码:using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// AjaxMethod 的摘要描述
/// </summary>
public class AjaxMethod
{
GetDataSet
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static DataSet GetCatList(string id)
{
string sql = "SELECT CAT_ID,CAT_NAME FROM CAT WHERE PARENT_ID='" + id.ToString() + "'";
return GetDataSet(sql);
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dropdownlist.aspx.cs" Inherits="Dropdownlist" EnableEventValidation="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DropDownList示例</title>
<script language="javascript">
//Cat联动部分
function catResult()
{
var bcat=document.getElementById("<%=ddlbCat.ClientID%>");
if (bcat.value != "")
{
AjaxMethod.GetCatList(bcat.value,get_cat_Result_CallBack);
}
else
{
document.all("<%=ddlCat.ClientID%>").length=0;
document.all("<%=ddlCat.ClientID%>").options.add(new Option("--Select--",""));
}
}
function get_cat_Result_CallBack(response)
{
if (response.value != null)
{
document.all("<%=ddlCat.ClientID%>").length=0;
document.all("<%=ddlCat.ClientID%>").options.add(new Option("--Select--",""));
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
for(var i=0; i<ds.Tables[0].Rows.length; i++)
{
var name=ds.Tables[0].Rows[i].CAT_NAME;
var id=ds.Tables[0].Rows[i].CAT_ID;
document.all("<%=ddlCat.ClientID%>").options.add(new Option(name,id));
}
}
}
return
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DropDownList ID="ddlbCat" runat="server" Width="80px"></asp:DropDownList>
<asp:DropDownList ID="ddlCat" runat="server" Width="80px"><asp:ListItem Value="">--Select--</asp:ListItem></asp:DropDownList>
</form>
</body>
</HTML>
五、Dropdownlist.aspx.cs代码:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DropDownList示例</title>
<script language="javascript">
//Cat联动部分
function catResult()
{
var bcat=document.getElementById("<%=ddlbCat.ClientID%>");
if (bcat.value != "")
{
AjaxMethod.GetCatList(bcat.value,get_cat_Result_CallBack);
}
else
{
document.all("<%=ddlCat.ClientID%>").length=0;
document.all("<%=ddlCat.ClientID%>").options.add(new Option("--Select--",""));
}
}
function get_cat_Result_CallBack(response)
{
if (response.value != null)
{
document.all("<%=ddlCat.ClientID%>").length=0;
document.all("<%=ddlCat.ClientID%>").options.add(new Option("--Select--",""));
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
for(var i=0; i<ds.Tables[0].Rows.length; i++)
{
var name=ds.Tables[0].Rows[i].CAT_NAME;
var id=ds.Tables[0].Rows[i].CAT_ID;
document.all("<%=ddlCat.ClientID%>").options.add(new Option(name,id));
}
}
}
return
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DropDownList ID="ddlbCat" runat="server" Width="80px"></asp:DropDownList>
<asp:DropDownList ID="ddlCat" runat="server" Width="80px"><asp:ListItem Value="">--Select--</asp:ListItem></asp:DropDownList>
</form>
</body>
</HTML>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Dropdownlist 的摘要说明。
/// </summary>
public partial class Dropdownlist : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if (!this.Page.IsPostBack)
{
//Load Controls
this.DDLBind(this.ddlbCat, "select [CAT_ID],[CAT_NAME] from CAT where PARENT_ID='0'");
this.ddlbCat.Attributes.Add("onclick", "catResult();");
}
}
//Load DropDownList
private void DDLBind(System.Web.UI.WebControls.DropDownList DDL, string SQLString)
{
string connectionString = ConfigurationManager.AppSettings["dbConnectionString"];
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
try
{
SqlCommand cmd = new SqlCommand(SQLString, connection);
SqlDataReader dr = cmd.ExecuteReader();
DDL.Items.Clear();
ListItem default_item = new ListItem();
default_item.Value = "";
default_item.Text = "--Select--";
DDL.Items.Add(default_item);
while (dr.Read())
{
ListItem ListItem = new ListItem();
ListItem.Value = dr.GetValue(0).ToString();
ListItem.Text = dr.GetString(1);
DDL.Items.Add(ListItem);
}
dr.Close();
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
//Load Selected DropDownList
public static void DDLOldBind(System.Web.UI.WebControls.DropDownList DDL, string sql, string Id)
{
DDL.Items.Clear();
ListItem default_item = new ListItem();
default_item.Value = "";
default_item.Text = "--Select--";
DDL.Items.Add(default_item);
try
{
DataTable dt = ExecuteQuery(sql);
if (dt.Rows.Count >= 1)
{
foreach (DataRow dr in dt.Rows)
{
ListItem ListItem = new ListItem();
ListItem.Value = dr[0].ToString();
ListItem.Text = dr[1].ToString();
DDL.Items.Add(ListItem);
}
}
DDL.SelectedValue = Id;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(DDL.ID.ToString() + E.Message.ToString());
}
}
}
六、Ajax.dll文件.using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Dropdownlist 的摘要说明。
/// </summary>
public partial class Dropdownlist : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if (!this.Page.IsPostBack)
{
//Load Controls
this.DDLBind(this.ddlbCat, "select [CAT_ID],[CAT_NAME] from CAT where PARENT_ID='0'");
this.ddlbCat.Attributes.Add("onclick", "catResult();");
}
}
//Load DropDownList
private void DDLBind(System.Web.UI.WebControls.DropDownList DDL, string SQLString)
{
string connectionString = ConfigurationManager.AppSettings["dbConnectionString"];
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
try
{
SqlCommand cmd = new SqlCommand(SQLString, connection);
SqlDataReader dr = cmd.ExecuteReader();
DDL.Items.Clear();
ListItem default_item = new ListItem();
default_item.Value = "";
default_item.Text = "--Select--";
DDL.Items.Add(default_item);
while (dr.Read())
{
ListItem ListItem = new ListItem();
ListItem.Value = dr.GetValue(0).ToString();
ListItem.Text = dr.GetString(1);
DDL.Items.Add(ListItem);
}
dr.Close();
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
//Load Selected DropDownList
public static void DDLOldBind(System.Web.UI.WebControls.DropDownList DDL, string sql, string Id)
{
DDL.Items.Clear();
ListItem default_item = new ListItem();
default_item.Value = "";
default_item.Text = "--Select--";
DDL.Items.Add(default_item);
try
{
DataTable dt = ExecuteQuery(sql);
if (dt.Rows.Count >= 1)
{
foreach (DataRow dr in dt.Rows)
{
ListItem ListItem = new ListItem();
ListItem.Value = dr[0].ToString();
ListItem.Text = dr[1].ToString();
DDL.Items.Add(ListItem);
}
}
DDL.SelectedValue = Id;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(DDL.ID.ToString() + E.Message.ToString());
}
}
}
Ajax.dll下载