曾经,我们为了实现dropdownlist的无刷新联动绞尽脑汁,最后还不得不写大量的js代码来实现。,如今,
微软的atlas,可以让我们摆脱写大段JS代码来实现dropdownlist的无刷新联动,而且简单易懂。
一、拖入一个dropdownlist1控件,
二、在drp.aspx页面上放一个atlas的核心控件updatepanel1,给updatepanel1增加一个scriptmanager控件,
注意scriptmanager的属性 EnablePartialRendering必须设为"True",一个页面只能有一个scriptmanager控件
三、将一个dropdownlist2控件拖入到updatepanel1区域内,并将其ispostback属性设为true ,
现在设置updatepanel1的属性triggers:updatepanel1 的control指定为dropdownlist1,proprety指定为selectvalue;
四、再放入一个updatepanel2,在里面再放一个dropdownlist3,
现在设置updatepanel2的属性triggers:updatepanel2 的control指定为updatepanel1,proprety指定为triggers;
因为dropdownlist3的数据源是dropdownlist2的索引改变而来,而dropdownlist2在updatepanel1里面,所以这里
是设置updatepanel2 的control为updatepanel1,proprety指定为triggers;
此时,所有的属性基本设置完毕了,开始写服务器端的代码
DropDownList1的索引改变事件
{
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("select * from test where bianhao='"+DropDownList1.SelectedValue+"'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList2.DataSource = ds.Tables[0];
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataBind();
}
DropDownList2的索引改变事件
{
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("select * from test where name='" + DropDownList2.SelectedValue + "'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList3.DataSource = ds.Tables[0];
DropDownList3.DataTextField = "address";
DropDownList3.DataValueField = "address";
DropDownList3.DataBind();
}
创建数据表SQL
drop table [ dbo ] . [ test ]
GO
CREATE TABLE [ dbo ] . [ test ] (
[ id ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ name ] [ char ] ( 10 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ address ] [ char ] ( 10 ) COLLATE Chinese_PRC_CI_AS NULL ,
[ bianhao ] [ char ] ( 10 ) COLLATE Chinese_PRC_CI_AS NULL
) ON [ PRIMARY ]
GO
全部文件代码
using System;
using System.Data;
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;
using System.Data.SqlClient;
public partial class Drp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("select * from test where bianhao='"+DropDownList1.SelectedValue+"'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList2.DataSource = ds.Tables[0];
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataBind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("select * from test where name='" + DropDownList2.SelectedValue + "'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList3.DataSource = ds.Tables[0];
DropDownList3.DataTextField = "address";
DropDownList3.DataValueField = "address";
DropDownList3.DataBind();
}
}
客户端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Drp.aspx.cs" Inherits="Drp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
</atlas:ScriptManager>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>01</asp:ListItem>
<asp:ListItem>02</asp:ListItem>
<asp:ListItem>03</asp:ListItem>
</asp:DropDownList>
<atlas:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<atlas:ControlValueTrigger ControlID="DropDownList1" PropertyName="SelectedValue" />
</Triggers>
</atlas:UpdatePanel>
<atlas:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<atlas:ControlValueTrigger ControlID="UpdatePanel1" PropertyName="Triggers" />
</Triggers>
</atlas:UpdatePanel>
<div>
</div>
</form>
</body>