代码之家  ›  专栏  ›  技术社区  ›  Keith Walton

如何从跨页回发的PreviousPage访问ViewState集合?

  •  3
  • Keith Walton  · 技术社区  · 17 年前

    在ASP.NET 2.0中,网页的PreviousPage属性没有ViewState集合。我想用这个集合在页面之间传递信息。

    4 回复  |  直到 15 年前
        1
  •  1
  •   StingyJack    17 年前

    视图状态是该页的独占状态。

    如果你想转移物品,

    • 您可以将数据保存在数据库、文件、表单身份验证票据或其他cookie中(如果可以帮助,请不要使用session或httpcontext.current.cache)
    • 进行跨页发布-从第一页发回第二页(并从httpcontext.current.request.form[]集合获取详细信息)
    • 将值放入查询字符串中
        2
  •  1
  •   FlySwat    17 年前

    请改用httpcontext.current.items…viewstate仅对其所在的页面有效。

        3
  •  1
  •   jekcom    15 年前

    通过使用可以保存对象的一些基本页类,可以避免使用previousPageType指令。 例如你有课

    public class BaseCrossPage:System.Web.UI.Page
    {
         public List<Guid> Invitees = new List<Guid>();
    
    }
    

    所以如果第一页是从这个类派生的

    public partial class Default : BaseCrossPage
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
           this.Invitees = LoadInvitees();
        }
    
    
    }
    

    然后,您发布到的页面可以访问该对象,假定上一页是从basecrosspage派生的…

    public partial class secondPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            BaseCrossPage p = (BaseCrossPage)PreviousPage;
            List<Guid> Invitees = p.InvitedTeams
        }
    }
    

    页面之间的“视图状态”…

        4
  •  0
  •   Keith Walton    17 年前

    你不能直接说。(见 http://msdn2.microsoft.com/en-us/library/ms178139(vs.80).aspx

    这是你能做的-

    在第一页上创建公共属性,公开要共享的信息。在第二页,将PreviousPageType设置为ASPX文件头中的第一页:

    <%@ previouspagetype virtualpath="~/firstpage.aspx" %>
    

    然后,在第二页的Load事件中获取这些属性的值:

    If (Not MyBase.IsPostBack) Then
    
        _someValue = Me.PreviousPage.SomeValue
    
    End If
    
    推荐文章