atlas实现三层dropdownlist无刷新联动

曾经,我们为了实现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的索引改变事件

None.gif protected   void  DropDownList1_SelectedIndexChanged( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
InBlock.gif        SqlConnection conn 
= new SqlConnection(strConn);
InBlock.gif        SqlDataAdapter da 
= new SqlDataAdapter("select * from test where bianhao='"+DropDownList1.SelectedValue+"'", conn);
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        da.Fill(ds);
InBlock.gif        DropDownList2.DataSource 
= ds.Tables[0];
InBlock.gif        DropDownList2.DataTextField 
= "name";
InBlock.gif        DropDownList2.DataValueField 
= "name";
InBlock.gif        DropDownList2.DataBind();
InBlock.gif      
ExpandedBlockEnd.gif    }

DropDownList2的索引改变事件
None.gif   protected   void  DropDownList2_SelectedIndexChanged( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
InBlock.gif        SqlConnection conn 
= new SqlConnection(strConn);
InBlock.gif        SqlDataAdapter da 
= new SqlDataAdapter("select * from test where name='" + DropDownList2.SelectedValue + "'", conn);
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        da.Fill(ds);
InBlock.gif
InBlock.gif        DropDownList3.DataSource 
= ds.Tables[0];
InBlock.gif
InBlock.gif        DropDownList3.DataTextField 
= "address";
InBlock.gif        DropDownList3.DataValueField 
= "address";
InBlock.gif        DropDownList3.DataBind();
InBlock.gif    
InBlock.gif
ExpandedBlockEnd.gif    }

创建数据表SQL
None.gif if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[test] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
None.gif
drop   table   [ dbo ] . [ test ]
None.gif
GO
None.gif
None.gif
CREATE   TABLE   [ dbo ] . [ test ]  (
None.gif 
[ id ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
None.gif 
[ name ]   [ char ]  ( 10 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
None.gif 
[ address ]   [ char ]  ( 10 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
None.gif 
[ bianhao ]   [ char ]  ( 10 ) COLLATE Chinese_PRC_CI_AS  NULL  
None.gif
ON   [ PRIMARY ]
None.gif
GO

全部文件代码
ContractedBlock.gif ExpandedBlockStart.gif drp.aspx.cs
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
None.gif
public partial class Drp : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
InBlock.gif        SqlConnection conn 
= new SqlConnection(strConn);
InBlock.gif        SqlDataAdapter da 
= new SqlDataAdapter("select * from test where bianhao='"+DropDownList1.SelectedValue+"'", conn);
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        da.Fill(ds);
InBlock.gif        DropDownList2.DataSource 
= ds.Tables[0];
InBlock.gif        DropDownList2.DataTextField 
= "name";
InBlock.gif        DropDownList2.DataValueField 
= "name";
InBlock.gif        DropDownList2.DataBind();
InBlock.gif      
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
InBlock.gif        SqlConnection conn 
= new SqlConnection(strConn);
InBlock.gif        SqlDataAdapter da 
= new SqlDataAdapter("select * from test where name='" + DropDownList2.SelectedValue + "'", conn);
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        da.Fill(ds);
InBlock.gif
InBlock.gif        DropDownList3.DataSource 
= ds.Tables[0];
InBlock.gif
InBlock.gif        DropDownList3.DataTextField 
= "address";
InBlock.gif        DropDownList3.DataValueField 
= "address";
InBlock.gif        DropDownList3.DataBind();
InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

客户端代码:
ContractedBlock.gif ExpandedBlockStart.gif html
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="Drp.aspx.cs" Inherits="Drp" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>无标题页</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif        
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
None.gif        
</atlas:ScriptManager>
None.gif        
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
None.gif            
<asp:ListItem>01</asp:ListItem>
None.gif            
<asp:ListItem>02</asp:ListItem>
None.gif            
<asp:ListItem>03</asp:ListItem>
None.gif        
</asp:DropDownList>
None.gif        
<atlas:UpdatePanel ID="UpdatePanel1" runat="server">
None.gif            
<ContentTemplate>
None.gif                
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
None.gif                
</asp:DropDownList>&nbsp;
None.gif            
</ContentTemplate>
None.gif            
<Triggers>
None.gif                
<atlas:ControlValueTrigger ControlID="DropDownList1" PropertyName="SelectedValue" />
None.gif            
</Triggers>
None.gif        
</atlas:UpdatePanel>
None.gif        
<atlas:UpdatePanel ID="UpdatePanel2" runat="server">
None.gif            
<ContentTemplate>
None.gif        
<asp:DropDownList ID="DropDownList3" runat="server">
None.gif        
</asp:DropDownList>
None.gif            
</ContentTemplate>
None.gif            
<Triggers>
None.gif                
<atlas:ControlValueTrigger ControlID="UpdatePanel1" PropertyName="Triggers" />
None.gif            
</Triggers>
None.gif        
</atlas:UpdatePanel>
None.gif    
<div>
None.gif        
&nbsp;
None.gif    
None.gif    
</div>
None.gif        
&nbsp; &nbsp;
None.gif    
</form>
None.gif
</body>

转载于:https://www.cnblogs.com/huazi4995/archive/2006/10/22/536438.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值