Vs2005中使用Ajax(二)【转】

摘要:
原始邮寄地址:http://www.blogjava.net/Microhelp/articles/147114.html1这里我们需要做的是一个省市三级联动的下拉列表框。效果如下所示。在上一个项目中,创建一个新页面AjaxForm2.aspx,添加三个下拉列表,添加一个文本框和一个按钮<table><tr><tdstyle=“62px”>province</td><tdstyle=“148p

原帖地址:http://www.blogjava.net/Microhelp/articles/147114.html

1. 这里要做的是一个省市区三级联动的下拉列表框,效果如下图。
Vs2005中使用Ajax(二)【转】第1张
在前面的项目中,新建页面
AjaxForm2.aspx,添加三个DropdownList,添加一个textbox和一个button

<table>

            <tr>

                <tdstyle=" 62px"></td>

                <tdstyle=" 148px">

                    <asp:DropDownListID="DropDownList1"runat="server"Width="133px"></asp:DropDownList>

                </td>

            </tr>

            <tr>

            <tdstyle=" 62px">城市</td>

                <tdstyle=" 148px">

                    <asp:DropDownListID="DropDownList2"runat="server"Width="131px"></asp:DropDownList>

                </td>

            </tr>

            <tr>

            <tdstyle=" 62px"></td>

                <tdstyle=" 148px">

                    <asp:DropDownListID="DropDownList3"runat="server"Width="129px"></asp:DropDownList>

                </td>

            </tr>

 </table>

asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

        <asp:ButtonID="Button1"runat="server"Text="Button"onclientclick="getData(); return false;"/></div>

2. sqlserver数据库中的tempdb数据库中建立三张表

Vs2005中使用Ajax(二)【转】第2张
Vs2005中使用Ajax(二)【转】第3张
Vs2005中使用Ajax(二)【转】第4张

3. Ajaxmethod.cs中添加如下方法,注意要using System.Data.SqlClient;

#region GetPovinceList

         publicstaticDataSet GetPovinceList()

         {

              string sql="select * from province";

              return GetDataSet(sql);

         }

 

     

         #endregion

 

         #region GetCityList

         [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]

         public DataSet GetCityList(int povinceid)

         {

              string sql="select * from city where father="+povinceid;

              return GetDataSet(sql);         

         }

         #endregion

 

         #region GetAreaList

         [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]

         public DataSet GetAreaList(int cityid)

         {

              string sql="select * from area where father="+cityid;

              return GetDataSet(sql);         

         }

         #endregion

    

         #region GetDataSet

         publicstaticDataSet GetDataSet(string sql)

          {

            //string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

            string ConnectionString = "Data Source=HOME;Initial Catalog=tempdb;User ID=sa;Password=sa";

              SqlDataAdapter     sda =newSqlDataAdapter(sql,ConnectionString);

              DataSet ds=newDataSet();

              sda.Fill(ds);

              return ds;

         }

         #endregion

 

        publicstaticDataSet GetCityListtt(int povinceid)

        {

            string sql = "select * from city where father=" + povinceid;

            return GetDataSet(sql); 

        }

 

4. pageload里添加如下方法

Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));

              if(!Page.IsPostBack)

              {

                this.DropDownList1.DataSource = AjaxMethod.GetPovinceList();

                this.DropDownList1.DataTextField = "province";

                this.DropDownList1.DataValueField = "provinceID";

                this.DropDownList1.DataBind();

 

                this.DropDownList2.DataSource = AjaxMethod.GetCityListtt(0);

                this.DropDownList2.DataTextField = "city";

                this.DropDownList2.DataValueField = "cityID";

                this.DropDownList2.DataBind();

                  

                   this.DropDownList1.Attributes.Add("onclick","cityResult();");

                   this.DropDownList2.Attributes.Add("onclick","areaResult();");

              }

5. 在页面的head中添加如下javascript代码

<scripttype="text/jscript">             

              //城市------------------------------

              function cityResult()

              {

                   var city=document.getElementById("DropDownList1");

                   AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);

              }

             

              function get_city_Result_CallBack(response)

              {

                   if (response.value != null)

                   {                     

                       //debugger;

                       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

              }

              //市区----------------------------------------

              function areaResult()

              {

                   var area=document.getElementById("DropDownList2");

                   AjaxMethod.GetAreaList(area.value,get_area_Result_CallBack);

              }

              function get_area_Result_CallBack(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));

                   }                 

                       }

                   }

                   return

              }

              function getData()

              {

                   var province=document.getElementById("DropDownList1");

                   var pindex = province.selectedIndex;

                   var pValue = province.options[pindex].value;

                   var pText = province.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;

                  

                   var txt=document.getElementById("TextBox1");                                  

 

                   document.getElementById("<%=TextBox1.ClientID%>").innerText="省:"+pValue+"|"+pText+"市:"+cValue+"|"+cText+"区:"+aValue+"|"+aText;

              }

         </script>

6. 好了,看一下效果吧,还可以吧?看一下Ajax是怎么工作的。Webconfig指定了postgethttp处理方法为ajax。在pageload的方法里注册了javascript使用的后台ajax的类。并且初始化了两个下拉框。当然这里可以把三个全部初始化。然后为一二两个下拉框的onlick事件添加了相应的javascrip处理方法。以cityResult为例。它调用了后台的AjaxMethod.GetCityList方法。由于该类在pageload的时候被注册了。所以前台可以调用这个方法。然后返回了一个dataset。并指明了回调函数为get_city_Result_CallBack。这样当后台数据处理结束之后回自动传到前台的get_city_Result_CallBack方法。这个方法对返回的数据集进行处理。便利获得的数据集,并且把数据添加到下一个列表框。感觉如何啊?

免责声明:文章转载自《Vs2005中使用Ajax(二)【转】》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Selenium2+python自动化6-八种元素元素定位(Firebug和firepath)【转载】038 Go操作etcd下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

.net webapi后台返回pdf文件流,前端ajax请求下载,空白pdf排错经历

.net webapi后台返回pdf文件流,前端ajax请求下载,空白pdf排错经历 先上代码: 后台webapi代码: [HttpGet] [Route("{manifestId}")] public IHttpActionResult FindManifestPdfById([FromUri]string manifestId) {     byte...

jquery分析之文件

一、文件分析1、从github中把jquery的源码全部下载下来,其中的src文件夹,是jquery的所有功能文件夹,jquery现在开始把功能都进行模块化开发,支持amd和cmd。 jquery-master>src 2、分析文件 ajax——ajax功能 attribute——选择器功能 core——核心代码 css——css操作 data——文...

ajax()函数传值中文乱码解决方法介绍

jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 复制代码 代码如下: $.ajax({   dataType : ‘json',type : ‘POST',url : ‘http://localhost/test/test.do',data : {id: 1, type: ‘商品'},success : functio...

laraver ajax分页

1,设置分页容器参考laraver手册 我的设置代码如下://设置分页容器  /app/models/ZurbPresenter.php<?php     class ZurbPresenter extends IlluminatePaginationPresenter {    public function getPageLinkWrapper...

js~ajax获取后端HTTP状态的几种情况

jquery发起ajax请求到后端接口,后端向前端返回数据,当然也存在后端接口直接重定向(302)到其它接口,然后再向前端返回数据,当然这种情况需要考虑跨域问题。 前端代码 //测试一下http status $.ajax({ type: "get", url: "/redirect",...

黑马lavarel教程---11、响应处理

黑马lavarel教程---11、响应处理 一、总结 一句话总结: 如果在昂扬状态,看学习视频和运动时间重合,会很舒服 1、jquery的$.post参数为什么不需要占位? 因为每个参数的类型不同,可以识别:$.post:$.post (地址,[参数对象,回调,期望返回类型]); 2、常见的ajax响应数据类型? 有json和xml、text/html,但...