实现无刷新闪烁二级联动下拉菜单


先建立2个表



-- 父表
create   table  tb_parent(
-- 主键
ids  int   constraint  pk_tb_parent_ids  primary   key ,
parentName 
nvarchar ( 1000 )
)
go

insert   into  tb_parent
    
select   1 , ' aaa '
    
union   all
    
select   2 , ' bbb '
    
union   all
    
select   3 , ' ccc '
go

 


-- 子表
create   table  tb_child(
parentId 
int   ,
childId 
int  ,
childName 
nvarchar  ( 1000 ),
-- parentId外键
constraint  fk_tb_child_tb_parent_parentId 
    
FOREIGN   KEY  (parentId)     
        
REFERENCES  tb_parent(ids)
)
go


insert   into  tb_child
    
select   1 , 101 , ' a_1 '
    
union   all
    
select   1 , 102 , ' a_2 '
go
insert   into  tb_child
    
select   2 , 201 , ' b_1 '
    
union   all
    
select   2 , 202 , ' b_2 '
go
insert   into  tb_child
    
select   3 , 301 , ' c_1 '
    
union   all
    
select   3 , 302 , ' c_2 '
    
union   all
    
select   3 , 303 , ' c_3 '
go

再创建3个过程


-- 得到父表数据
create   proc  proc_GetparentData
as
SELECT   [ ids ] [ parentName ]  
    
FROM   [ tb_parent ]
go


-- 得到子表数据
create   proc  proc_GetchildData
as
SELECT   [ parentId ] [ childId ] [ childName ]  
    
FROM   [ tb_child ]
go

-- 由父id得到子表数据
create   proc  proc_GetchildDataBYparentId
@parentId   int
as
SELECT   [ parentId ] [ childId ] [ childName ]  
    
FROM   [ tb_child ]
    
where  parentId = @parentId
go

WebForm5.aspx

 1  <% @ Page language = " c# "  Codebehind = " WebForm5.aspx.cs "  AutoEventWireup = " false "  Inherits = " webtest.WebForm5 "   %>
 2  <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
 3  < HTML >
 4       < HEAD >
 5           < title > WebForm5 </ title >
 6           < meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
 7           < meta  content ="C#"  name ="CODE_LANGUAGE" >
 8           < meta  content ="JavaScript"  name ="vs_defaultClientScript" >
 9           < meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
10       </ HEAD >
11       < body  MS_POSITIONING ="GridLayout" >
12           < form  id ="Form1"  method ="post"  runat ="server" >
13              父: < asp:dropdownlist  id ="DropDownList_parent"  runat ="server"  onChange ="changevalue(document.Form1.DropDownList_parent.options[document.Form1.DropDownList_parent.selectedIndex].value)"
14                  Width ="272px" ></ asp:dropdownlist >
15               < br >
16              子: < asp:dropdownlist  id ="DropDownList_child"  runat ="server"  Width ="272px" ></ asp:dropdownlist >
17               < br >
18               < asp:label  id ="msgLabel"  runat ="server"  Width ="416px" ></ asp:label >
19               < br >
20               < asp:Button  id ="Buttonok"  runat ="server"  Text ="click" ></ asp:Button ></ form >
21       </ body >
22  </ HTML >

WebForm5.aspx.cs

  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Data;
  5 None.gif using  System.Data.SqlClient;
  6 None.gif using  System.Drawing;
  7 None.gif using  System.Web;
  8 None.gif using  System.Web.SessionState;
  9 None.gif using  System.Web.UI;
 10 None.gif using  System.Web.UI.WebControls;
 11 None.gif using  System.Web.UI.HtmlControls;
 12 None.gif using  System.Text;
 13 None.gif
 14 None.gif using  Microsoft.ApplicationBlocks.Data;
 15 None.gif
 16 None.gif namespace  webtest
 17 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 18InBlock.gif    public class WebForm5 : System.Web.UI.Page
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 20InBlock.gif        protected System.Web.UI.WebControls.DropDownList DropDownList_parent;
 21InBlock.gif        protected System.Web.UI.WebControls.DropDownList DropDownList_child;
 22InBlock.gif        
 23InBlock.gif        protected System.Web.UI.WebControls.Label msgLabel;
 24InBlock.gif        protected System.Web.UI.WebControls.Button Buttonok;
 25InBlock.gif
 26InBlock.gif        readonly string conString="uid=sa;pwd=123;database=TestDataBase";
 27InBlock.gif    
 28InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 30InBlock.gif                regJS();
 31InBlock.gif                Bind();
 32ExpandedSubBlockEnd.gif        }

 33InBlock.gif
 34InBlock.gif        private void regJS()
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 36InBlock.gif            SqlDataReader rs=this.GetchildData();
 37InBlock.gif            StringBuilder sb=new StringBuilder(1000);
 38InBlock.gif
 39InBlock.gif            sb.Append("<Script Language=JavaScript>");
 40InBlock.gif            sb.Append(Environment.NewLine);
 41InBlock.gif            
 42InBlock.gif            sb.Append("arr=new Array();");
 43InBlock.gif            sb.Append(Environment.NewLine);
 44InBlock.gif            
 45InBlock.gif            int i=0;
 46InBlock.gif            while(rs.Read())
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 48InBlock.gif                sb.AppendFormat("arr[{0}]=new Array('{1}','{2}','{3}')",i,rs["parentId"],rs["childId"],rs["childName"]);
 49InBlock.gif                sb.Append(Environment.NewLine);
 50InBlock.gif                i=i+1;
 51ExpandedSubBlockEnd.gif            }

 52InBlock.gif            
 53InBlock.gif            if ( !rs.IsClosed )
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                rs.Close();
 56ExpandedSubBlockEnd.gif            }

 57InBlock.gif
 58InBlock.gif            sb.Append(Environment.NewLine);
 59InBlock.gif            sb.AppendFormat("var counts={0}",i);
 60InBlock.gif            sb.Append(Environment.NewLine);
 61InBlock.gif            sb.Append("function changevalue(parentId)");
 62InBlock.gif            sb.Append(Environment.NewLine);
 63InBlock.gif            sb.Append("{");
 64InBlock.gif            sb.Append(Environment.NewLine);
 65InBlock.gif            sb.Append("document.Form1.DropDownList_child.length = 0;");
 66InBlock.gif            sb.Append(Environment.NewLine);
 67InBlock.gif            sb.Append("var i;");
 68InBlock.gif            sb.Append(Environment.NewLine);
 69InBlock.gif            sb.Append("for(i=0; i<counts; i++)");
 70InBlock.gif            sb.Append(Environment.NewLine);
 71InBlock.gif            sb.Append("{");
 72InBlock.gif            sb.Append(Environment.NewLine);
 73InBlock.gif            sb.Append("if(arr[i][0]==parentId)");
 74InBlock.gif            sb.Append(Environment.NewLine);
 75InBlock.gif            sb.Append("{");
 76InBlock.gif            sb.Append(Environment.NewLine);
 77InBlock.gif            sb.Append("document.Form1.DropDownList_child.options[document.Form1.DropDownList_child.length]=new Option(arr[i][2],arr[i][1]);");
 78InBlock.gif            sb.Append(Environment.NewLine);
 79InBlock.gif            sb.Append("}");
 80InBlock.gif            sb.Append(Environment.NewLine);
 81InBlock.gif            sb.Append("}");
 82InBlock.gif            sb.Append(Environment.NewLine);
 83InBlock.gif            sb.Append("}");
 84InBlock.gif            sb.Append(Environment.NewLine);
 85InBlock.gif            sb.Append("</script>");            
 86InBlock.gif                        
 87InBlock.gif            if!Page.IsClientScriptBlockRegistered("jsScript"))            
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 89InBlock.gif                this.RegisterClientScriptBlock("jsScript",sb.ToString());
 90ExpandedSubBlockEnd.gif            }

 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93InBlock.gif        void Bind()
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95InBlock.gif            //获得父表
 96InBlock.gif            this.DropDownList_parent.DataSource=SqlHelper.ExecuteReader(conString,CommandType.StoredProcedure,"proc_GetparentData");
 97InBlock.gif            this.DropDownList_parent.DataTextField="parentName";
 98InBlock.gif            this.DropDownList_parent.DataValueField="ids";
 99InBlock.gif            this.DropDownList_parent.DataBind();
100InBlock.gif
101InBlock.gif            //根据父表id得子表
102InBlock.gif            this.DropDownList_child.DataSource=GetchildData(Convert.ToInt32(this.DropDownList_parent.SelectedValue));
103InBlock.gif            this.DropDownList_child.DataTextField="childName";
104InBlock.gif          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值