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

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();
      
    }

DropDownList2的索引改变事件
 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();
    

    }

创建数据表SQL
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[test]'and OBJECTPROPERTY(id, N'IsUserTable'= 1)
drop table [dbo].[test]
GO

CREATE TABLE [dbo].[test] (
 
[id] [int] IDENTITY (11NOT 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

全部文件代码
drp.aspx.cs

客户端代码:
html
posted on 2006-10-22 15:13  huazi4995  阅读(576)  评论(0编辑  收藏  举报