后台:
首先添加一个using AjaxPro;引用
然后加上 [AjaxPro.AjaxNamespace("TextVerify")]这个标记,并在窗体的加载事件里面加上AjaxPro.Utility.RegisterTypeForAjax(typeof(页面名字));
后台:
StringBuilder strHtml = new StringBuilder();
strHtml.Append("<a οnclick=/"GetPId('" + dt.Rows[i]["productID"].ToString() + "')/"><img src=/"../images/buttons_coll.jpg/" /></a>"); //动态加载的,这里的GetPId方法调用的是前台的
下面是我做的一个收藏产品的方法:
/// <summary>
/// 收藏产品方法
/// </summary>
[AjaxPro.AjaxMethod]
public string GetSave(string pid)
{
string str=null;
Poucan.BLL.CustomerFavorites bll = new Poucan.BLL.CustomerFavorites();
Poucan.Model.CustomerFavorites model = new Poucan.Model.CustomerFavorites();
if (PageProduct.getCustomerName() != null)
{
if (bll.Exists(int.Parse(pid), PageProduct.getCustomerName()) == false)
{
model.Sorts = "1";
model.pid = int.Parse(pid);
model.Customer = PageProduct.getCustomerName();//用户
int ID = bll.Add(model); //收藏的方法
str = ID.ToString();
str = "1";
}
else
{
str = "2";
}
}
else
{
str = "3";
}
return str;
}
前台代码:
function GetPId(pid) {
var SaveProduct = TextVerify.GetSave(pid,callback);
}
//回调函数
function callback(aa)
{
if (aa.value == "1") {
alert("收藏成功!");
}
else if (aa.value == "2") {
alert("商品已存在,不能重复收藏!!");
}
else if (aa.value == "3") {
alert("不能收藏,请登录.........", window.location = "/User/login.aspx");
}
}