博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用户控件(areacontrol)
阅读量:5236 次
发布时间:2019-06-14

本文共 8071 字,大约阅读时间需要 26 分钟。

1 Ajax.dll 的应用

2 ajax 函数的实现(1  无法调用服务器控件,如给服务器控件赋值  获取服务器状态等   若需以上操作  通过ajax函数的参数来传值到后台  2  要有返回值  由回调函数来处理  )。

3 回调函数 :处理后台返回的值 ,对ajax函数执行的结果进行处理等。

4 用户控件中的服务器控件   后台给定的id在前台无法使用  (设置clienetidmode等方式   或者 <%=ddl1.clientid %>)

5  xml 字符串构造

6  javascript 加载ajax函数返回的结果(xml 字符串) 。

 

 

 

 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="area.ascx.cs" Inherits="CControl.userControl.areaControl" %>

<script type="text/javascript">    

var str = "";

    function SetAreaId(ClassID) {        

var hiddenID = document.getElementById("HiddenId");     

    hiddenID.value = ClassID;   

      var HiddenText = document.getElementById("HiddenText");

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

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

     var drp3 = document.getElementById("DropDownList3");    

     if (drp1.selectedIndex > -1) {       

      HiddenText.value = drp1.options[drp1.selectedIndex].text;   

          if (drp2 != null && drp2.selectedIndex > -1) {       

          HiddenText.value += "-" + drp2.options[drp2.selectedIndex].text;         

        if (drp3 != null && drp3.selectedIndex > -1) {               

      HiddenText.value += "-" + drp3.options[drp3.selectedIndex].text;           

      }      

       }   

      }

 

    }

    function loadsecond(ClassID) {      

     //ClassID为接收传递的大类编号      

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

    SetAreaId(ClassID);    

     if (drp2 != null) {      

       var drp3 = document.getElementById("DropDownList3");     

        RemoveAll(drp2);

            if (drp3 != null)          

       RemoveAll(drp3);

            str = areaControl.bindsecond(ClassID, callbackcomplete);   

      }    

}    

function callbackcomplete(res) {       

  var oDoc = new ActiveXObject("MSXML2.DOMDocument");  

       oDoc.loadXML(res.value);  

       items1 = oDoc.selectNodes("/ClassLists/List/name");     

    items2 = oDoc.selectNodes("/ClassLists/List/id");   

      var itemsLength = items1.length;  

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

        for (i = 0; i < itemsLength; i++) {      

       var newOption = document.createElement("OPTION");       

      newOption.text = items1[i].text;     

        newOption.title = items1[i].text;     

        newOption.value = items2[i].text;        

     drp2.options.add(newOption);     

    }

    }   

  function RemoveAll(oElem) {         

    //清除DropDownList2的所有项       

  var i = 0;     

    if (oElem != null)        

     for (i = oElem.length; i >= 0; i--) {         

        oElem.options.remove(i);       

      }     }    

 

function loadthird(ClassID) {      

     //ClassID为接收传递的大类编号     

    var drp3 = document.getElementById("DropDownList3");

        SetAreaId(ClassID);     

    if (drp3 != null) {  

           RemoveAll(drp3);      

       str = areaControl.Bindthird(ClassID, callbackthirdcomplete);     

    }     }   

 

  function callbackthirdcomplete(res) {       

  var oDoc = new ActiveXObject("MSXML2.DOMDocument");     

    oDoc.loadXML(res.value);   

      items1 = oDoc.selectNodes("/ClassLists/List/name");     

    items2 = oDoc.selectNodes("/ClassLists/List/id");         var itemsLength = items1.length;     

    var drp3 = document.getElementById("DropDownList3");    

     for (i = 0; i < itemsLength; i++) {       

      var newOption = document.createElement("OPTION");     

        newOption.text = items1[i].text;      

       newOption.title = items1[i].text;       

      newOption.value = items2[i].text;           

  drp3.options.add(newOption);     

    }

    }

</script>

<ul><li><div style="float:left">

    省<asp:DropDownList ID="DropDownList1" Width="150px"  runat="server" ClientIDMode="Static" οnchange="javascript:loadsecond(this.options[this.selectedIndex].value);">     </asp:DropDownList>    </div></li>    

    <li><div style="float:left">  

   <asp:Panel runat="server" ID="City" >   

    市<asp:DropDownList ID="DropDownList2"  Width="150px" runat="server" ClientIDMode="Static" οnchange="javascript:loadthird(this.options[this.selectedIndex].value);">         </asp:DropDownList>             </asp:Panel> </div></li> <li> <div style="float:left">         <asp:Panel runat="server" ID="Town">        

     县<asp:DropDownList ID="DropDownList3"  Width="150px"  runat="server" ClientIDMode="Static" οnchange="javascript:SetAreaId(this.options[this.selectedIndex].value);">             </asp:DropDownList>         </asp:Panel>        </div>        </li> </ul>

  <asp:HiddenField ID="HiddenId" ClientIDMode="Static" runat="server" />  

  <asp:HiddenField ID="HiddenText" ClientIDMode="Static" runat="server" />

 

 

后台代码:

namespace CControl.userControl {     /// <summary>     /// 显示地区列表数量,最多三级,add by clare     /// </summary>   

  public partial class areaControl : System.Web.UI.UserControl     {       /// <summary>    

     /// 显示的地区层数 最多3层,默认3层     

    /// </summary>   

      public int Floor         {       

      get     

        {           

      if (ViewState["_floor"] == null)             

        return 3;        

         else               

      return (int)ViewState["_floor"]; ;      

       }      

       set {    

             if(value>0 && value<4)             

        ViewState["_floor"] = value;          

       else          

           ViewState["_floor"] = 3;     

        }         }

        /// <summary>         /// 二级是否添加"全部"选择条,默认是         /// </summary>       

  public bool SecondNeedZero     

    {        

     get       

      {             

    if (ViewState["_SecondZeroItem"] == null)            

         return true;        

         else             

        return (bool)ViewState["_SecondZeroItem"];      

       }          

   set { ViewState["_SecondZeroItem"] = value; }  

       }

 

 

        /// <summary>         /// 三级是否添加全部选择,默认是         /// </summary>     

    public bool ThirdNeedZero         {       

      get             {       

          if (ViewState["_thirdNeedZero"] == null)          

           return true;          

       else              

       return (bool)ViewState["_thirdNeedZero"];        

     }         

    set { ViewState["_thirdNeedZero"] = value; }         }

        /// <summary>         /// 首级地区从电信端列表里读取,默认false;         /// </summary>    

     public bool FirstFromTelecom         {         

    get {          

       if (ViewState["_firstFromTelecom"] == null)       

              return false;    

             else             

        return (bool)ViewState["_firstFromTelecom"];         

         }        

     set { ViewState["_firstFromTelecom"] = value; }    

     }    

     public int GetAreasID         {       

      get {            

     if (string.IsNullOrEmpty(this.HiddenId.Value))        

             return 0;             

    return int.Parse(this.HiddenId.Value);             }            

        }    

 

 

     /// <summary>         /// 返回地区名,多级累加的         /// </summary>   

      public string AreasName     

    {     

        get { return this.HiddenText.Value; }            

        }         //private  bool _secondFromHotel = false;      

 

   / <summary>         / 二级菜单是否从酒店列表里读取,若是,则FirstFromTelecom必须为true         / </summary>         //public bool SecondFromHotel         //{         //    get { return _secondFromHotel; }         //    set { _secondFromHotel = value; }         //}        

 

 

protected void Page_Load(object sender, EventArgs e)      

   {        

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

       if (!IsPostBack)       

      {         

        switch (Floor)         

        {             

        case 1:                         City.Visible = false;              

           //DropDownList2.Attributes.Add("display", "none");             

            //DropDownList3.Attributes.Add("display", "none");           

              Town.Visible = false;          

               break;           

          case 2:               

          Town.Visible = false;               

          //DropDownList3.Attributes.Add("display", "none");          

               break;            

         default :                       

                        break;   

              }

            }     

        bindFirst();

        }    

 

 

     protected void bindFirst()    

     {         

    DropDownList1.Items.Clear();    

         if (FirstFromTelecom)      

       {           

      PubInfo.BLL.SRC_T_Telcom bll = new PubInfo.BLL.SRC_T_Telcom();         

        DataSet ds = bll.GetTelcomList();        

         foreach (DataRow item in ds.Tables[0].Rows)           

      {               

      //DropDownList1.DataSource = ds;            

         //DropDownList1.DataTextField = "Name";           

          //DropDownList1.DataValueField = "areaId";              

       //DropDownList1.SelectedIndex = -1;               

      try                     {         

                DropDownList1.Items.Add(new ListItem(item["Name"].ToString(), item["areaId"].ToString()));           

          }                     catch { }     

            }                  

           }     

        else       

      {          

       PubInfo.BLL.Area bll = new PubInfo.BLL.Area();             

    int total=0;                 ///这个函数.GetProvince有问题,需要修改         

        List<PubInfo.Model.Area> models= bll.GetProvinceAreas(0,0,out total);   

              foreach (PubInfo.Model.Area item in models)       

          {         

            DropDownList1.Items.Add(new ListItem(item.AreaName,item.ID.ToString()));        

         }    

         }            

foreach (ListItem item in DropDownList1.Items)       

      {           

      item.Attributes["title"] = item.Text;     

        }      

       DropDownList1.Items.Insert(0,new ListItem("请选择...", "-1"));       

      DropDownList1.SelectedIndex = -1;   

      }     

    [Ajax.AjaxMethod]    

     public string bindsecond(int P)       

  {                    

    StringBuilder sb = new StringBuilder();         

    sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");   

          sb.Append("<ClassLists>");      

       List<PubInfo.Model.Area> areas = new List<PubInfo.Model.Area>();     

        int total = 0;                 ///这个调用的函数也是有问题的,没有 处理pageSize=0的情况      

           areas = new PubInfo.BLL.Area().GetTownAreas(P, 0, 0, out total);        

         if (SecondNeedZero)         

        {          

           sb.AppendFormat("<List><id>{0}</id><name>{1}</name></List>", -1, "请选择...");      

           }      

           foreach (PubInfo.Model.Area item in areas)         

        {        

 

             sb.AppendFormat("<List><id>{0}</id><name>{1}</name></List>", item.ID, item.AreaName);        

         }

            sb.Append("</ClassLists>");

            return sb.ToString();  

       }      

   [Ajax.AjaxMethod]     

    public string Bindthird(int P)   

      {          

   StringBuilder sb = new StringBuilder();       

      sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");     

        sb.Append("<ClassLists>");       

      List<PubInfo.Model.Area> areas = new List<PubInfo.Model.Area>();  

           int total = 0;             ///这个调用的函数也是有问题的,没有 处理pageSize=0的情况     

        areas = new PubInfo.BLL.Area().GetTownAreas(P, 0, 0, out total);     

        if (ThirdNeedZero)         

    {       

          sb.AppendFormat("<List><id>{0}</id><name>{1}</name></List>", -1, "请选择");    

         }     

        foreach (PubInfo.Model.Area item in areas)       

      {             

    sb.AppendFormat("<List><id>{0}</id><name>{1}</name></List>", item.ID, item.AreaName);        

     }

            sb.Append("</ClassLists>");

            return sb.ToString();     

    }  

   }

}

转载于:https://www.cnblogs.com/joyosblogs/p/3384251.html

你可能感兴趣的文章
MyBatis课程2
查看>>
桥接模式-Bridge(Java实现)
查看>>
网络请求返回HTTP状态码(404,400,500)
查看>>
Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
查看>>
Mac下使用crontab来实现定时任务
查看>>
303. Range Sum Query - Immutable
查看>>
图片加载失败显示默认图片占位符
查看>>
【★】浅谈计算机与随机数
查看>>
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
Atlas命名空间Sys.Data下控件介绍——DataColumn,DataRow和DataTable
查看>>
Java中正则表达式的使用
查看>>
算法之搜索篇
查看>>
新的开始
查看>>
java Facade模式
查看>>
NYOJ 120校园网络(有向图的强连通分量)(Kosaraju算法)
查看>>
Leetcode 226: Invert Binary Tree
查看>>
http站点转https站点教程
查看>>
解决miner.start() 返回null
查看>>
bzoj 2007: [Noi2010]海拔【最小割+dijskstra】
查看>>