如何使用ajax和pagemethods更新页面变量?
有没有办法从页面方法更新实例变量而不将其设为静态?
我的页面有一个 ViewModel 对象,其中包含我想要更新 ajax 和 pagemethods 的大量数据。我在asp.net前端页面中使用ViewModel来打印变量值。
ViewModel 类
// ViewModel class
public class ItemViewModel
{
public List<Item> Items = new List<Item>();
}
** ASP.net webforms 页面(后端)**
public ItemViewModel ViewModel;
protected void Page_Load(object sender, EventArgs e)
{
// init ViewModel;
}
[WebMethod]
public static bool GetItems(string userId)
{
// lots of code...
ItemService i = new ItemService();
ViewModel = i.GetItems(userId); // How to update page variables or pass in a new one?
return true;
}
前端
<ul>
<% foreach (Item i in ViewModel.Items)
{ %>
<li>
<%=i.ItemName %>
</li>
<% } %>
</ul>
Is there any way to update instance variables from a page method without making them static?
My page has a ViewModel object that contains lots of data that I would like to update ajax and pagemethods. I use the ViewModel in the asp.net front-end page to print out variable values.
ViewModel Class
// ViewModel class
public class ItemViewModel
{
public List<Item> Items = new List<Item>();
}
** ASP.net webforms page (backend)**
public ItemViewModel ViewModel;
protected void Page_Load(object sender, EventArgs e)
{
// init ViewModel;
}
[WebMethod]
public static bool GetItems(string userId)
{
// lots of code...
ItemService i = new ItemService();
ViewModel = i.GetItems(userId); // How to update page variables or pass in a new one?
return true;
}
Front-end
<ul>
<% foreach (Item i in ViewModel.Items)
{ %>
<li>
<%=i.ItemName %>
</li>
<% } %>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用会话状态是可能的,因为它是在多个页面上进行的,但是您必须确保当用户在任何给定时间切换页面时,会话数据被清除,因为否则它将在下一个页面加载时使用(除非是你想要的)。
您还可以让您的方法构建一个粘贴到页面中的 htmlstring:
然后在 ajax
success
回调中您可以执行以下操作:它可以工作,但它确实需要您手动构建一个 html 字符串,如果您必须处理多个一维数组,这会变得更加困难。
我建议将 JSON 与 Ajax 结合使用。这是传递您想要的任何内容的最简单方法,并且仍然能够处理数据,而不仅仅是将其粘贴为 html。
http://www.learn-ajax-tutorial.com/Json.cfm
我建议阅读本教程,它可以让您很好地了解 JSON 的构建和处理方式。
Using Session state is possible as it is carried over multiple pages, but you will have to make sure that when a user switches a pages at any given time, the Session-data is cleared because else it will be used on next pageload (unless that is what you want).
You could also have your method build a htmlstring that is pasted into your page:
And then in your ajax
success
callback you can do:It works, but it does require you to build a html string by hand, which gets exponentially harder if you have to handle more than a onedimensional array.
I would suggest using JSON in combination with Ajax. It's the easist way to pass whatever you want, and still be able to handle the data rather than just have it pasted as html.
http://www.learn-ajax-tutorial.com/Json.cfm
I would suggest reading through this tutorial, it gives you a good idea on how JSON is built and handled.
正如您所注意到的,PageMethods 是静态的。如果没有实例可供使用,静态方法就无法操作实例成员。
此外,您必须了解请求完成后您的页面实例不存在。没有要操作的实例成员。
As you have noted, PageMethods are static. There is no way for a static method to manipulate instance members without having an instance to work with.
Furthermore, you have to understand that your page instance doesn't exist after the request has completed. There are no instance members to manipulate.
如果您需要将状态信息从一个请求转发到另一个请求,则无法使用 Page 实例变量来实现这一点(Page 实例仅在单个请求的持续时间内存在),并且您也不应该使用静态变量。
一种正确的方法是让 Ajax 等代码更新会话状态。如果您不能或不想使用会话状态,还有其他可能性。
If you need to carry state information forward from one request to another, you can't do that with Page instance variables (a Page instance only exists for the duration of a single request)--and you shouldn't use statics, either.
One correct approach is to have your Ajax, etc, code update Session state. There are other possibilities if you can't or don't want to use Session state.