ajax2级联动,遇到一个异步优先级问题,使用settimeout 解决

页面:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AreaSelect.ascx.cs" Inherits="CorpUserRegister_AreaSelect" %>
<script type="text/javascript">
       function getDiqu()
       {
            var cityID 
= document.getElementById("AreaSelect1_drp_Chengshi").value;
            CorpUserRegister_AreaSelect.getDiqu(cityID,getDiquRes);
       }
       function getDiquRes(res)
       {
            
if(res.value != null)
            {
                var dt 
= res.value;
                var ddlQu 
= document.getElementById("AreaSelect1_drp_Qu");
                
if(dt != null && typeof(dt) == "object")
                {
                    
//清理列表项
                    ddlQu.length=0;
                    ddlQu.options.add(
new Option('请选择','-1'));
                    
for(var i = 0;i < dt.Rows.length;i++)
                    {
                        var name 
= dt.Rows[i].Title;
                        var id 
= dt.Rows[i].SortCode;//这里对应数据表中的字段名
                        ddlQu.options.add(new Option(name,id));
                    }
                }
                
else
                {
                    ddlQu.options.add(
new Option("数据读取错误,请刷新页面",-1));
                }
            }
       }
       
       var ddl ;
       var theIndex;
       var theValue;   
       var theText;
       function postDiqu()
       {
            var a 
= new Array(4);
            getElem(
"AreaSelect1_drp_Chengshi");
            a[
0]=theText;
            a[
1]=theValue;
            getElem(
"AreaSelect1_drp_Qu");
            a[
2]= theText;
            a[
3]=theValue;
            var hiddenValue 
= document.getElementById('AreaSelect1_HiddenValue').value=a;
            document.getElementById(
'AreaSelect1_HiddenCityName').value=a[0];
            document.getElementById(
'AreaSelect1_HiddenCityID').value=a[1];
            document.getElementById(
'AreaSelect1_HiddenQuName').value=a[2];
            document.getElementById(
'AreaSelect1_HiddenQuID').value=a[3];
            CorpUserRegister_AreaSelect.postDiqu(a,postDiquRes);
       }
       function postDiquRes(res)
       {
            
//alert(res.value);
       }
       function getElem(obj)
       {
             ddl 
= document.getElementById(obj);
             theIndex 
=  ddl.selectedIndex;
             theValue 
=  ddl.options[theIndex].value;   
             theText 
=   ddl.options[theIndex].text;
       }
       onload
=function() 
       {
           var isPostChengshi 
= document.getElementById('AreaSelect1_HiddenCityID').value;
           getElem(
"AreaSelect1_drp_Chengshi");
           
for(i=0;i<ddl.options.length;i++)
           {
            
if(ddl.options[i].value==isPostChengshi)
             {
                ddl.selectedIndex 
= i;
             }
           }
           
if(isPostChengshi>0)
           {
            getDiqu();
            
//延时等待获取2级列表完成,即使时间设置为0也是必须有的,是为了降低异步排序优先级  20090617
            setTimeout(getPostBackQu,0);
           }
           
else
           {
             getPostBackQu();
           }
           
       }
       function getPostBackQu()
       {
           var isPostBackQu 
= document.getElementById('AreaSelect1_HiddenQuID').value;
           getElem(
"AreaSelect1_drp_Qu");
           
for(i=0;i<ddl.options.length;i++)
           {
             
if(ddl.options[i].value==isPostBackQu)
             {
                ddl.selectedIndex 
= i;
             }
           }
       }
</script>

<asp:DropDownList runat="server" ID="drp_Chengshi">
<asp:ListItem Value="-1">选择</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="drp_Qu" ckCode="10" runat="server" CssClass="sample" flow="1">
</asp:DropDownList>
<asp:HiddenField ID="HiddenValue" runat="server" />
<asp:HiddenField ID="HiddenCityName" runat="server" />
<asp:HiddenField ID="HiddenCityID" runat="server" />
<asp:HiddenField ID="HiddenQuName" runat="server" />
<asp:HiddenField ID="HiddenQuID" runat="server" />


 

cs:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 使用说明
/// 
/// 若要获取整个字符串
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenValue"))).Value
/// 获取到的字符串分别依次是 城市名,城市id,区县名,区县ID,请split
/// 
/// 若需要独立的值
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenCityName"))).Value
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenCityID"))).Value
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenQuName"))).Value
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenQuID"))).Value
/// 
/// 若要给dropdowlist赋初始值:在隐藏域中赋值,会被dropdownlist获取到
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenCityName"))).Value = 值
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenCityID"))).Value = 值
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenQuName"))).Value = 值
/// ((HiddenField)(this.AreaSelect1.FindControl("HiddenQuID"))).Value = 值
/// </summary>
public partial class CorpUserRegister_AreaSelect : System.Web.UI.UserControl
{
    
////通过protected的变量,向page传递参数
    //public  string _cityName = string.Empty;
    
//public  string _cityID = string.Empty;
    
//public  string _cityQuName = string.Empty;
    
//public  string _cityQuID = string.Empty;


    
protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(
typeof(CorpUserRegister_AreaSelect));
        
this.drp_Chengshi.Attributes.Add("onchange""getDiqu();");
        
this.drp_Qu.Attributes.Add("onchange""postDiqu();");
        
//取默认的城市
        ViewState["S_AreaCode"= FlowDll.GetSysDefaultAreaCode( );
        
if (!Page.IsPostBack)
        {
            bindCity();
            bindQu();
        }
        
else
        {
            bindQu(); 
        }
    }
    
private void bindCity()
    {
        DataView dr 
= new UserRegisterOp().BindDdl_Area().DefaultView;
        
this.drp_Chengshi.DataSource = dr;
        drp_Chengshi.DataTextField 
= "Title";
        drp_Chengshi.DataValueField 
= "Code";
        drp_Chengshi.DataBind();
        drp_Chengshi.SelectedValue 
= ViewState["S_AreaCode"].ToString();
        drp_Chengshi.Items.Insert(
0new ListItem("选择""-1"));

        
//判断本地是否可选
        try
        {
            drp_Chengshi.Enabled 
= bool.Parse(System.Configuration.ConfigurationSettings.AppSettings["bLocalAreaCodeEdit"].ToString());
        }
        
catch
        {
            
//若没有特别定义,都可选
            drp_Chengshi.Enabled = true;
        }
    }
    
/// <summary>
    
/// 邦定本市的区
    
/// </summary>
    private void bindQu()
    {
        DataView dr 
= new UserRegisterOp().BindDdl_SubArea(drp_Chengshi.SelectedValue.Trim()).DefaultView;
        drp_Qu.DataSource 
= dr;
        drp_Qu.DataTextField 
= "title";
        drp_Qu.DataValueField 
= "sortcode";
        drp_Qu.DataBind();
        drp_Qu.Items.Insert(
0new ListItem("请选择""-1"));
    }

    [AjaxPro.AjaxMethod]
    
public DataView getDiqu(string cityID)
    {
        DataView dv 
= new UserRegisterOp().BindDdl_SubArea(cityID).DefaultView;
        
return dv;
    }
    
////向公共变量中传递值
    [AjaxPro.AjaxMethod]
    
public string postDiqu(string[] options)
    {
        
return ViewState["diqu"].ToString();
    }
    
}

 

 

 

转载于:https://www.cnblogs.com/terrorpig/archive/2009/06/17/1505163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值