欢迎来到福编程网,本站提供各种互联网专业知识!

Ajax实现无刷新三联动下拉框

发布时间:2007-04-28 作者: 来源:转载
Ajax实现无刷新三联动下拉框//城市------------------------------functioncityResult(){varcity=document.getElementById("DropDownList1");AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);}functionget_city_Result_CallBack(response){if


Ajax实现无刷新三联动下拉框





//城市------------------------------
functioncityResult()
{
varcity=document.getElementById("DropDownList1");
AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);
}

functionget_city_Result_CallBack(response)
{
if(response.value!=null)
{
//debugger;
document.all("DropDownList2").length=0;
vards=response.value;
if(ds!=null&&typeof(ds)=="object"&&ds.Tables!=null)
{
for(vari=0;i{
varname=ds.Tables[0].Rows[i].city;
varid=ds.Tables[0].Rows[i].cityID;
document.all("DropDownList2").options.add(newOption(name,id));
}
}
}
return
}
//市区----------------------------------------
functionareaResult()
{
vararea=document.getElementById("DropDownList2");
AjaxMethod.GetAreaList(area.value,get_area_Result_CallBack);
}
functionget_area_Result_CallBack(response)
{
if(response.value!=null)
{
document.all("DropDownList3").length=0;
vards=response.value;
if(ds!=null&&typeof(ds)=="object"&&ds.Tables!=null)
{
for(vari=0;i{
varname=ds.Tables[0].Rows[i].area;
varid=ds.Tables[0].Rows[i].areaID;
document.all("DropDownList3").options.add(newOption(name,id));
}
}
}
return
}
functiongetData()
{
varprovince=document.getElementById("DropDownList1");
varpindex=province.selectedIndex;
varpValue=province.options[pindex].value;
varpText=province.options[pindex].text;

varcity=document.getElementById("DropDownList2");
varcindex=city.selectedIndex;
varcValue=city.options[cindex].value;
varcText=city.options[cindex].text;

vararea=document.getElementById("DropDownList3");
varaindex=area.selectedIndex;
varaValue=area.options[aindex].value;
varaText=area.options[aindex].text;

vartxt=document.getElementById("TextBox1");

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




cellPadding="1"width="300"border="1"bgColor="#ccff66">

省市



城市



市区



Width="424px">type="button"value="test"onclick="getData();">


2.cs代码
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespaceAjaxTest
{
/**////


///SummarydescriptionforWebForm1.
///

publicclassWebForm1:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.DropDownListDropDownList1;
protectedSystem.Web.UI.WebControls.DropDownListDropDownList2;
protectedSystem.Web.UI.WebControls.TextBoxTextBox1;
protectedSystem.Web.UI.WebControls.DropDownListDropDownList3;

privatevoidPage_Load(objectsender,System.EventArgse)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if(!Page.IsPostBack)
{
this.DropDownList1.DataSource=AjaxMethod.GetProvinceList();
this.DropDownList1.DataTextField="province";
this.DropDownList1.DataValueField="provinceID";
this.DropDownList1.DataBind();

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

WebFormDesignergeneratedcode#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}

/**////


///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///

privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);

}
#endregion
}
}3.AjaxMethod
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
namespaceAjaxTest
{
/**////


///SummarydescriptionforAjaxMethod.
///

publicclassAjaxMethod
{
GetProvinceList#regionGetProvinceList
publicstaticDataSetGetProvinceList()
{
stringsql="select*fromprovince";
returnGetDataSet(sql);
}
#endregion

GetCityList#regionGetCityList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
publicDataSetGetCityList(intprovinceid)
{
stringsql="select*fromcitywherefather="+provinceid;
returnGetDataSet(sql);
}
#endregion

GetAreaList#regionGetAreaList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
publicDataSetGetAreaList(intcityid)
{
stringsql="select*fromareawherefather="+cityid;
returnGetDataSet(sql);
}
#endregion

GetDataSet#regionGetDataSet
publicstaticDataSetGetDataSet(stringsql)
{
stringConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdaptersda=newSqlDataAdapter(sql,ConnectionString);
DataSetds=newDataSet();
sda.Fill(ds);
returnds;
}
#endregion
}
}4.web.config


5.ajax.dll下载/Files/singlepine/Ajax.rar

相关推荐