ASP.NET无刷新二级联动下拉列表
来源:互联网 发布:apache ab下载 编辑:程序博客网 时间:2024/05/16 01:09
可能"极好的"又会带来很多的非议,但是我认为这确实很好,我看了大约20个无刷新的连动下拉列表,他们在firefox下面就一团糟.为了这个我差不多搞了两天,就是如果提交窗体后如何保持第二个列表框的值,因为通过js 给下拉框添加条目那么他的状态是不会被保存的测试平台:ie6,firefox功能:二级无刷新连动特点:跨浏览器;提交窗体取第二下拉框的值;数据来源于数据库;以xmlhttp来发送请求,实现无刷新请求:如果您能够找到更好的方法请告诉我,非常感谢,您的批评和建议对我是莫大的鼓励webform1.aspx:<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="drop.WebForm1" %><!DOCTYPE HTML PUBLIC "- ><HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language="javascript">function jb( ){ var A=null; try { A=new ActiveXObject( "Msxml2.XMLHTTP" ); } catch( e ) { try { A=new ActiveXObject( "Microsoft.XMLHTTP" ); } catch( oc ) { A=null } } if (!A && typeof XMLHttpRequest != "undefined") { A=new XMLHttpRequest( ) } return A}function Go( obj ){ var svalue = obj.value; var weburl = "webform1.aspx?parent_id="+svalue; var xmlhttp = jb( ); xmlhttp.open( "get",weburl,true ); xmlhttp.onreadystatechange=function( ) { if( xmlhttp.readyState==4 ) { var result = xmlhttp.responseText; document.getElementById( "dListChild" ).length = 0; document.getElementById( "dListChild" ).options.add( new Option( "全部型号","0" ) ); if( result!="" ) { var allArray = result.split( "," ); for( var i=1; i<allArray.length; i++ ) { var thisArray = allArray[i].split( "|" ); document.getElementById( "dListChild" ).options.add( new Option( thisArray[1].toString( ), thisArray[0].toString( ) ) ); } } } } xmlhttp.send( null );}</script> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"><asp:DropDownList onchange="Go( this )" id="dListParent" style="Z-INDEX: 101;LEFT: 248px;POSITION: absolute;TOP: 40px"runat="server"><asp:ListItem Value="100">摩托罗拉</asp:ListItem><asp:ListItem Value="101">诺基亚</asp:ListItem></asp:DropDownList><asp:DropDownList id="dListChild" style="Z-INDEX: 102;LEFT: 248px;POSITION: absolute;TOP: 104px"runat="server"></asp:DropDownList><asp:Button id="Button1" style="Z-INDEX: 103;LEFT: 256px;POSITION: absolute;TOP: 176px" runat="server"Text="Button"></asp:Button> </form> </body></HTML>后台webform1.aspx.cs:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Configuration;using System.Data.SqlClient;namespace drop{ public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList dListParent; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DropDownList dListChild; private void Page_Load( object sender, System.EventArgs e ) { { BindDrop( ); } } protected void BindDrop( ) { { } string str = Request.QueryString["parent_id"]; string str1 = dListParent.SelectedValue; ; Response.Write( str1 ); if( ( str+"abc" )!="abc" ) { BindChild( str ); } else BindParent( str1 ); } protected void BindParent( string str ) { int int i = Convert.ToInt32( str ); dListChild.Items.Clear( ); dListChild.Items.Add( new ListItem( "全部型号","0" ) ); string connStr = ConfigurationSettings.AppSettings["ConnString"].ToString( ); SqlConnection conn = new SqlConnection( connStr ); string commStr = string.Format( "select type_value,type_text from phone_type where parent_id={0}",i ); SqlCommand comm = new SqlCommand( commStr,conn ); conn.Open( ); SqlDataReader dr = comm.ExecuteReader( ); while( dr.Read( ) ) { dListChild.Items.Add( new ListItem( dr[1].ToString( ),dr[0].ToString( ) ) ); } dListParent.Items[0].Selected = true; dListChild.SelectedValue = Request.Form["dListChild"]; dr.Close( ); conn.Close( ); } protected void BindChild( string str ) { int int i = Convert.ToInt32( str ); string result = ""; Response.Clear( ); string connStr = ConfigurationSettings.AppSettings["ConnString"].ToString( ); SqlConnection conn = new SqlConnection( connStr ); SqlCommand comm = conn.CreateCommand( ); string commStr = string.Format( "select type_value,type_text from phone_type where parent_id = {0}",i ); comm.CommandText = commStr; conn.Open( ); SqlDataReader dr = comm.ExecuteReader( ); while( dr.Read( ) ) { result += ","+dr[0].ToString( ) +"|" + dr[1].ToString( ); } Response.Write( result ); Response.Flush( ); Response.Close( ); dr.Close( ); conn.Close( ); } #region Web 窗体设计器生成的代码 override protected void OnInit( EventArgs e ) { InitializeComponent( ); base.OnInit( e ); } private void InitializeComponent( ) { this.Button1.Click += new System.EventHandler( this.Button1_Click ); this.Load += new System.EventHandler( this.Page_Load ); } #endregion private void Button1_Click( object sender, System.EventArgs e ) { Response.Write( Request.Form["dListChild"].ToString( ) ); } }}数据表:主键idparent_id( int )type_value( int )type_text( varchar )int递增 父下拉框的value值 下拉框的value 下拉框的text