zoukankan      html  css  js  c++  java
  • AJAX实现无刷新三联动下拉框

    1、webconfig设置
         (1)在system.web区添加:
       
    <httpHandlers>
       
    <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro" />
      
    </httpHandlers>
       
       (2)在configuration区添加数据库连接字符串:
    <add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\Test\AjaxTest\DB\db.mdb;User ID='admin';" />

    2、html代码:
    <%@ Page language="c#" Codebehind="DropDownList.aspx.cs" AutoEventWireup="false" Inherits="AjaxTest.DropDownList" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
        
    <HEAD>
            
    <title>DropDownList</title>
            
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            
    <meta content="C#" name="CODE_LANGUAGE">
            
    <meta content="JavaScript" name="vs_defaultClientScript">
            
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
            
    <script language="javascript" type="text/javascript">
                function cityResult()
    //城市
                {
                    var city 
    = document.getElementById( "DropDownList1" );                
                    AjaxTest.AjaxMethod.GetCityList( city.value,callback_GetCityList )            
                }

                function callback_GetCityList( response )
    //回调
                {                
                    
    if( response.value != null )
                    
    {
                        document.all( 
    "DropDownList2" ).length = 0;
                        var ds 
    = response.value;                    
                        
    if( ds != null && typeof( ds ) == "object" && ds.Tables != null )
                        
    {
                            
    for( var i = 0; i< ds.Tables[0].Rows.length; i ++ )
                            
    {
                                var name 
    = ds.Tables[0].Rows[i].city;
                          var id 
    = ds.Tables[0].Rows[i].cityID;
                          document.all(
    "DropDownList2").options.add( new Option( name,id ) );
                            }

                        }

                    }

                    
    return areaResult();
                }

                function areaResult()
    //县,区
                {
                    var area 
    = document.getElementById( "DropDownList2" );
                    AjaxTest.AjaxMethod.GetAreaList( area.value,callback_GetAreaList );
                }

                function callback_GetAreaList( response )
                
    {
                    
    if( response.value != null )
                    
    {
                        document.all( 
    "DropDownList3" ).length = 0;
                        var ds 
    = response.value;                    
                        
    if( ds != null && typeof( ds ) == "object" && ds.Tables != null )
                        
    {
                            
    for( var i = 0; i< ds.Tables[0].Rows.length; i ++ )
                            
    {
                                var name 
    = ds.Tables[0].Rows[i].area;
                          var id 
    = ds.Tables[0].Rows[i].areaID;
                          document.all(
    "DropDownList3").options.add( new Option( name,id ) );
                            }

                        }

                    }

                                
                }

                function GetData()
                
    {
                    var provnice 
    = document.getElementById( "DropDownList1" );
                    var pindex 
    = provnice.selectedIndex;                
                    var pvalue 
    = provnice.options[pindex].value;
                    var ptext 
    = provnice.options[pindex].text;
                    
                    var city 
    = document.getElementById( "DropDownList2" );
                    var cindex 
    = city.selectedIndex;
                    var cvalue 
    = city.options[cindex].value;
                    var ctext 
    = city.options[cindex].text;
                    
                    var area 
    = document.getElementById( "DropDownList3" );
                    var aindex 
    = area.selectedIndex;
                    var avalue 
    = area.options[aindex].value;
                    var atext 
    = area.options[aindex].text;                
                    
                    document.getElementById( 
    "TextBox1" ).innerText = "省:" + ptext + "||  市:" + ctext + "||  县,区: " + atext;
                    
                }

            
    </script>
        
    </HEAD>
        
    <body>
            
    <form id="Form1" method="post" runat="server">
                
    <P><asp:dropdownlist id="DropDownList1" runat="server" Width="150px"></asp:dropdownlist><br>
                    
    <asp:dropdownlist id="DropDownList2" runat="server" Width="150px"></asp:dropdownlist><br>
                    
    <asp:dropdownlist id="DropDownList3" runat="server" Width="150px"></asp:dropdownlist><br>
                    
    <br>
                    
    <INPUT type="button" value="显示所选数据" onclick="GetData();">
                    
    <asp:TextBox id="TextBox1" runat="server" Width="688px"></asp:TextBox></P>
                
    <P>
                    
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
            
    </form>
        
    </body>
    </HTML>

    3、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;

    namespace AjaxTest
    {
        
    /// <summary>
        
    /// DropDownList 的摘要说明。
        
    /// </summary>

        public class DropDownList : System.Web.UI.Page
        
    {
            
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
            
    protected System.Web.UI.WebControls.DropDownList DropDownList2;
            
    protected System.Web.UI.WebControls.TextBox TextBox1;
            
    protected System.Web.UI.WebControls.Button Button1;
            
    protected System.Web.UI.WebControls.DropDownList DropDownList3;
        
            
    private void Page_Load(object sender, System.EventArgs e)
            
    {
                AjaxPro.Utility.RegisterTypeForAjax( 
    typeof( AjaxMethod ) );
                
    // 在此处放置用户代码以初始化页面
                if!IsPostBack )
                
    {
                    
    this.DropDownList1.DataSource = AjaxMethod.GetPovinceList();
                    
    this.DropDownList1.DataTextField = "province";
                    
    this.DropDownList1.DataValueField = "provinceID";
                    
    this.DropDownList1.DataBind();

                    
    this.DropDownList1.Attributes.Add( "onchange","cityResult();" );
                    
    this.DropDownList2.Attributes.Add( "onchange","areaResult();" );                
                }

            }


            
    Web 窗体设计器生成的代码

            
    private void Button1_Click(object sender, System.EventArgs e)
            
    {                        
                
    string aa = Request.Form["DropDownList1"]+ Request.Form["DropDownList2"+ Request.Form["DropDownList3"]; 
            }

        }

    }


    4、AjaxMethod方法类库:
    using System;
    using System.Data;
    using System.Data.OleDb;

    namespace AjaxTest
    {
        
    /// <summary>
        
    /// AjaxMethod 的摘要说明。
        
    /// </summary>

        public class AjaxMethod
        
    {
            
    public AjaxMethod()
            
    {
                
    //
                
    // TODO: 在此处添加构造函数逻辑
                
    //
            }

            
    GetPovinceList

            
    GetCityList

            
    GetAreaList
        
            
    GetDataSet

        }
    //end class
    }


    5、数据库文件:
    /Files/wander/db.rar
  • 相关阅读:
    [转载] 长草颜文字的写给未来
    [彩蛋题] egg
    最近发现一些项目ignore文件没有生效,请使用下面方式清理下Cache
    freemarker显示含有html代码的内容
    数字化技术促进电网转型发展
    停更的时间里,我也在好好生活和工作
    iOS 开发问题集锦(一)
    SVN 在 Xcode中的状态说明
    virt-v2v命令将ESXI 虚机迁移到OpenStack中
    修改openstack镜像--支持root密码登陆
  • 原文地址:https://www.cnblogs.com/wander/p/AJAX.html
Copyright © 2011-2022 走看看