• 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
  • 相关阅读:
    Eclipse 常用快捷键
    asp.net中如何计算中英文混合字符串长度
    图片水印的类库
    开源asp.net SNS源码下载地址
    eclipse修改编辑器字体 + Eclipse自动提示(Content Assist) + Eclipse汉化
    eclipse内部Javadoc怎么汉化成中文
    Eclipse设置输入智能提示
    Oracle建立表空间和用户
    王福朋asp.net2.0 petshop4.0系列视频教程的下载地址
    【转】在Eclipse中设置中文JavaDOC
  • 原文地址:https://www.cnblogs.com/wander/p/AJAX.html
Copyright © 2020-2023  润新知