【问题标题】:Saving the current running aspx page保存当前运行的 aspx 页面
【发布时间】:2014-11-14 09:17:37
【问题描述】:

我有一个带有在服务器上运行的表的 aspx 页面。我编写了一个代码来在表格中插入一行,如下面的代码所示。我想保存现在更新的 aspx 页面(即,如果可能,覆盖当前页面),以便下次访问该页面时,所有信息都会显示。我不想使用数据库来存储这些信息,因为这只是临时的,而且我的托管包不支持数据库。请帮忙。

下面是我的代码:

 Dim trow As New HtmlTableRow
    Dim tcell As New HtmlTableCell

    tcell.InnerText = "Code: " & TextBox1.Text & ".   Weight: " & TextBox2.Text & ". Date: " & Now
    trow.Cells.Add(tcell)

    trow.Cells.Add(tcell)

    Table.Rows.Add(trow)
    TextBox1.Text = ""
    TextBox2.Text = ""
    'Save the aspx page
    Me.SaveControlState()
    Me.SaveViewState()

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    您可以将数据保存在 cookie 中。试试这个。

    Writing a cookie.
    
    HttpCookie myCookie = new HttpCookie("MyTestCookie");
    DateTime now = DateTime.Now;
    
    // Set the cookie value.
    myCookie.Value = now.ToString();
    // Set the cookie expiration date.
    myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire
    
    // Add the cookie.
    Response.Cookies.Add(myCookie);
    
    Response.Write("<p> The cookie has been written.");
    

    读取 cookie

    HttpCookie myCookie = new HttpCookie("MyTestCookie");
    myCookie = Request.Cookies["MyTestCookie"];
    
    // Read the cookie information and display it.
    if (myCookie != null)
        Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
    else
        Response.Write("not found");
    

    【讨论】:

    • 这是个好主意,但我真的不知道如何使用它。我还是一个学习者。这就是我使用您的代码的方式: Dim myCookie As New HttpCookie("MyTestCookie") myCookie = Request.Cookies("MyTestCookie") ' 读取 cookie 信息并显示它。 If myCookie IsNot Nothing Then Response.Write(("

      " + myCookie.Name & "

      ") + myCookie.Value) Else Response.Write("not found") End If Else Response.Redirect("login .aspx") 结束如果

    【解决方案2】:

    使用会话:

    访问一个值:

    System.Web.HttpContext.Current.Session(“MyVariable”).ToString()
    

    设置一个值:

    System.Web.HttpContext.Current.Session(“MyVariable”) = “NewValue”
    

    【讨论】:

    • OP 希望数据在下次访问时保持不变,因此 Session 变量将不起作用
    猜你喜欢
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2014-07-23
    • 2015-10-07
    相关资源
    最近更新 更多