为了改维 www.pingco.com的联动效果,费了半天劲。使用AjaxPro2.dll 注意点:
1. web.config 检查是否有:必须有Ajax接管httpHandlers
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
2.在。cs中注册AjaxPro
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default)); //注册ajaxPro,括号中的参数是当前的类名
if (!Page.IsPostBack)
{
//---------------------------------------------------------------------------
this.DropDownList1.DataSource = getMobileType();
this.DropDownList1.DataTextField = "MobileFactory";
this.DropDownList1.DataValueField = "MobileFactory";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0, "-请选择手机品牌-");
this.DropDownList3.Items.Insert(0, "-请选择手机型号-");
this.DropDownList1.Attributes.Add("onchange", "mobileResult();");
}
}
/// <summary>
/// 获取手机类型
/// </summary>
/// <returns></returns>
public static DataTable getMobileType()
{
webcode.Data.SqlConn conn = new webcode.Data.SqlConn(System.Configuration.ConfigurationManager.AppSettings["ConnStr_Mobile"].ToString());
string cmdText = "select distinct(MobileFactory) from T***";
DataTable dt = conn.getDateTable(cmdText);
return dt;
}
/// <summary>
/// 获取手机型号
/// </summary>
/// <returns></returns>
//[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]//todo: 测试用
[AjaxPro.AjaxMethod]
public DataTable getMobileTypeNumber1(string MobileFactory)
{
string cmdText = "select distinct(MobileType) as MobileType, max(WapVersion) as WapVersion from T*** with(nolock) where MobileFactory='" + MobileFactory + "' group by MobileType order by MobileType desc";
webcode.Data.SqlConn conn = new webcode.Data.SqlConn(System.Configuration.ConfigurationManager.AppSettings["ConnStr_Mobile"].ToString());
DataTable dt = conn.getDateTable(cmdText);
return dt;
}
}
3.前台html代码:
<script language="javascript">
function mobileResult()
{
var m=document.getElementById("DropDownList1");
PingCoWeb._Default.getMobileTypeNumber1(m.value,mobileResult_callback);
}
function mobileResult_callback(response)
{
if (response.value != null)
{
document.getElementById("DropDownList3").length=0;
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds != null)
{
for(var i=0; i<ds.Rows.length; i++)
{
var name=ds.Rows[i].MobileType;
var id=ds.Rows[i].WapVersion;
document.all("DropDownList3").options.add(new Option(name,id));
}
}
}
return
}
</script>
在此一定要注意:PingCoWeb._Default.getMobileTypeNumber1(m.value,mobileResult_callback);
不能写成:_Default.getMobileTypeNumber1(m.value,mobileResult_callback);
否则会报js错误:"_Default” 未定义。 因为
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PingCoWeb._Default"
EnableEventValidation="false" %>
如果在类文件中定义getMobileTypeNumber1()函数,js中也要写上完整的类(加上命名空间。)
比如写到:PingCo.Origin.Web.Helper.AdvertiseDownLoad 下:
}
}
}
cs中注册为:
AjaxPro.Utility.RegisterTypeForAjax(typeof(AdvertiseDownLoad)); //注册ajaxPro,括号中的参数是当前的类名
js中就的写:PingCo.Origin.Web.Helper.AdvertiseDownLoad.getMobileTypeNumber1(m.value,mobileResult_callback);
【附录】