[英]Need help - MasterPage always return null when call from WebMethod (ASP.NET) C#
我正在构建一个购物车,一切正常。 现在,我正在尝试使用 jQuery 插入和显示数据库中的数据。 我使用 jQuery 成功插入数据,但我无法更新 MasterPage 内的面板,因为 master 总是返回 null。
有没有人有任何想法来解决我的问题?
<script>
$(document).ready(function () {
$("[id*=lb1KgButton]").click(function () {
$.ajax({
data: JSON.stringify({
customerID: _customerID
}),
dataType: "json",
type: "POST",
url: '<%= ResolveUrl("~/AddToCart.aspx/AddToCartFrom1To4Kg") %>',
contentType: "application/json; charset=utf-8",
success: function (result) {
showSuccess('', 'Added to cart successfully!');
}
});
});
});
</script>
这是 AddToCart.aspx 中的代码
[WebMethod]
public static string AddToCartFrom1To4Kg(string customerID)
{
string result = "";
AddToCart atc = new AddToCart();
atc.UpdateShoppingCart(); }
public void UpdateShoppingCart()
{
var master = Master as Home;
if (master != null)
{
UpdatePanel upMasterPage = (UpdatePanel)master.FindControl("UpdatePanel1");
master.CheckAddToCart();
upMasterPage.Update();
}
}
您不能从 static 方法更新页面。 您必须返回到 javascript 方法并从那里更新页面。 根据需要将数据/信息返回给 ajax 调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.