[英]Need help - MasterPage always return null when call from WebMethod (ASP.NET) C#
I am building a shopping cart, and everything works fine.我正在构建一个购物车,一切正常。 Now, I am trying to use jQuery to insert and show data from the database.
现在,我正在尝试使用 jQuery 插入和显示数据库中的数据。 I use jQuery to insert data successfully, but I cannot update the panel which is inside MasterPage, because the master always returns null.
我使用 jQuery 成功插入数据,但我无法更新 MasterPage 内的面板,因为 master 总是返回 null。
Does anyone have any idea to solve my issue?有没有人有任何想法来解决我的问题?
<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>
Here are code inside AddToCart.aspx这是 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();
}
}
You cannot update a page from a static method.您不能从 static 方法更新页面。 You must return to the javascript method and update the page from there.
您必须返回到 javascript 方法并从那里更新页面。 Return data/info to the ajax call as needed.
根据需要将数据/信息返回给 ajax 调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.