English 中文(简体)
重新张贴到同一页时, 查看状态错误无效
原标题:Invalid viewstate error when posting back to same page

我对一个 Invalid Viewstate 错误有些问题,我可以理解为什么会发生,但我不知道如何纠正。

我有一个与此 < code>/ story/?? id=123 相似的页面,但我使用不同的页面到 < code> server. 将 传送到此页面 。

所以我把 /info 设置为 server.transferrequent ("/story/?id=123") 。 它的工作正常, 直到页面返回自己。

我们在此页面上有一个登录格式, 只需重新装入页面即可, 但当它似乎将 < code> /? id=123 添加到 URL 结尾处时, 它就会像 < code>/ info/? id=123 那样结束, 从而造成 < em> 无效查看状态 < / em > 错误 。

我已经尝试过添加 EnableViewStateMac="false" - 这修正了错误, 但是它没有按照预期登录用户, 所以没有给出所需的结果 。

所以,我的问题是:

  1. 除了 server. Transferrequent 之外,是否有更好的方法可以重定向到我的页面,但是仍然保留这个不错的 URL? - 我不想 Response.repirect ,如果我能避免它的话。

  2. 如果不是,是否有一个简单的方法可以修正此错误, 不需要我添加 EnableViewStateMac= "false" ?

最佳回答

http://support.microsoft.com/kb/316920

我相信那篇文章会解释你为什么会有问题, 它提供了解决问题的解决方案。

我知道你不想使用回应。 调整方向, 但我认为这也解决了问题。

PRB: "View State Is Invalid" Error Message When You Use Server.Transfer

这篇文章以前在Q316920中发表

Retired KB Content Disclaimer

This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

SYMPTOMS

When you use HttpServerUtility.Transfer("page name", true), you receive the following error message:

查看国对本页面无效, 并可能损坏

CAUSE

This problem occurs because the EnableViewStateMac attribute of the <pages> element is set to true by default. When this attribute is set to true, ASP.NET runs a message authentication check (MAC) on the view state of the page when the page is posted back from the client. This check determines if the view state of the page was modified on the client. For security purposes, it is recommended that you keep this attribute set to true.

When you call the Server.Transfer method and set the second parameter to true, you preserve the QueryString and the Form collections. One of the form fields is the hidden __VIEWSTATE form field, which holds the view state for the page. The view state message authentication check fails because the message authentication check only checks each page. Therefore, the view state from the page that calls Server.Transfer is not valid on the destination page.

View state is page scoped and is valid for that page only. View state should not be transferred across pages.

RESOLUTION

为解决这一问题,应采用以下方法之一。

Resolution 1

Transfer values between pages to pass your server control values to the other pages. For more information, refer to the following MSDN documentation: Passing Server Control Values Between Pages This requires that you create public properties for each property of a control that you want to access from the destination page.

If you have many controls, and if you want to access the properties of these controls from another page, you can also declare those controls as public variables. For example:

< 强 > page1.aspx

Public Class Page1
    Public WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

     Insert your code here.
End Class

<强 > page2.aspx

Dim sourcePage As Page1
sourcePage = CType(Context.Handler, WebForm1)
Response.Write(sourcePage.TextBox1.Text)

Resolution 2

Do not pass the second parameter (which is false by default) when you call Server.Transfer. For example:

Server.Transfer("<page name>")

This code does not send the QueryString and the Form fields to the page that is called. When no data is transferred, ASP.NET does not run the message authentication check.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. 创建名为 WebForm1.aspx 的.aspx 页面, 将执行传输到另一页。 添加以下代码到 WebForm1.aspx :

    <%@ Page language="vb" AutoEventWireup="true" %>
    
    <html>  
      <body>  
        <form id="WebForm1" method="post" runat="server">
          <asp:TextBox id="txtName" runat="server">Your Name</asp:TextBox><br>
          <asp:Button id="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></asp:Button>
        </form>   
      </body>
    </html>
    
    <script runat=server>
    Sub Button1_Click(sender As Object, e As System.EventArgs)
      Server.Transfer("WebForm2.aspx",true)
    End Sub
    
    </script>
    
  2. 另创建名为 WebForm2.aspx 的.aspx 页面, 然后添加以下代码 :

    <%@ Page language="vb" AutoEventWireup="true" %>
    
    <html>
      <body>  
        <form id="WebForm2" method="post" runat="server">
          <asp:Label id="lblName" runat="server" >Web Form 2</asp:Label>
        </form>
    
      </body>
    </html>
    
    <script runat=server>
    Sub Page_Load(sender As Object, e As EventArgs)
    
    Dim thisPage As System.Web.UI.Page
    Dim nameTextBox As TextBox
    
      thisPage = CType(Context.Handler, System.Web.UI.Page)
      nameTextBox =  CType(thisPage.FindControl("txtName"), System.Web.UI.Control)
    
      lblName.Text = "Your name is  " & nameTextBox.Text & " ."   
    
    End Sub
    
    </script>
    
  3. 在浏览器中打开 WebForm1.aspx, 然后单击 < 坚固> submit 。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签