this.DropDownList1.Attributes.Add("onclick","cityResult();"); this.DropDownList2.Attributes.Add("onclick","areaResult();"); }WebForm1.aspx html代码: <SCRIPT language="javascript"> //城市------------------------------ 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)); } } } areaResult(); 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;
我也要一份
[email protected]
另外,在WebConfig中
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>写在</system.web>内!建立一个类:
using System;
using System.Data;
using System.Data.SqlClient;
namespace AjaxTest
{
/// <summary>
/// Summary description for AjaxMethod.
/// </summary>
public class AjaxMethod
{
#region GetPovinceList
public static DataSet GetPovinceList()
{
string sql="select * from povince";
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
public static DataSet GetDataSet(string sql)
{
string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
}
}WebForm1.aspx.cs代码中
Page_load:
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.DropDownList1.Attributes.Add("onclick","cityResult();");
this.DropDownList2.Attributes.Add("onclick","areaResult();");
}WebForm1.aspx html代码:
<SCRIPT language="javascript">
//城市------------------------------
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));
}
}
}
areaResult();
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>
为了能在页面加载的时候,就能显示出 省,市,区,
因此
<body ms_positioning="GridLayout" onload="cityResult();">
<%@ Import Namespace="你的名称空间"%>
还有想问问get_city_Result_CallBack(response)这句有什么用意啊?
加我QQ122397592
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public void text()
{
Response.Write("qqqqqqqqqqqqqqq");
}在Html里加入这些代码:
function go()
{
AjaxMethod.text();
}
然后在页面里加入个按钮调用go(),他没任何反应,把AjaxMethod.text()改成Webform1.text()状态栏就提示页面错误。请问怎样可以调用啊
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));//做标记另外,有没有在HTML里标记名称空间!
比如:
你在前台输入一个 aaa ,用Ajax传入后台,那样可以!
但是,你在后台return "aaaa";那么你想在前台通过Ajax接到这个值,然后输出是不行的!
namespace AjaxTest
{
/// <summary>
/// WebTest 的摘要说明。
/// </summary>
public class WebTest : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText txtPwd;
protected System.Web.UI.HtmlControls.HtmlInputText txtName;
private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(WebTest));
} [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public void Test(string txt)
{
txtPwd.Value=txt;
}
}
}前台:
function Name()
{
var txt=document.getElementById("txtName");
WebTest.Test(txt.value);
}
目的:让txtPwd文本框的值等于txtName的值,没有反应郁闷
http://blog.csdn.net/ChengKing/archive/2006/08/22/1106342.aspx
2.AjaxPro.Net example:
http://blog.csdn.net/ChengKing/archive/2006/08/22/1106456.aspx
http://blog.csdn.net/ChengKing/archive/2006/08/22/1106390.aspx
2、config文件里注册
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
2、Page_Load里
Ajax.Utility.RegisterTypeForAjax(typeof(类名));
3、定义它的一个方法
[Ajax.AjaxMethod()]
public string HelloAJAX(string pp)
{
return pp + "0000";
}
4、前台使用
<script language="javascript">
var testValue = 类名.HelloAJAX("xj");
DivShow.innerHTML = testValue.value;
</script> 按照上面的设定不会出错!