简体   繁体   中英

viewstate is not loading/missing for default.aspx

My default page isn't including the viewstate when doing the postback, ie the viewstate is missing, it's not being included in either the forms or params collections when I look at them during debugging. This also causes the IsPostBack property to show as False and the events associated with the postback to not be called.

My first thought was that somewhere viewstate was being turned off, but I have set it explicitly on the page and all of the controls (the page is extremely small, just 1 button and one list links). Still no luck. The funny thing is, this is only happening on the initial load of the page, if I follow one of the links out and then back to the default page, the viewstate is present.

Why would the viewstate not be created or generated? I'm not getting any validation errors. The first symptom I noticed was server events not being fired, but that made sense one I saw that the viewstate wasn't present, I just don't know why it is missing. I do have a form on the page, and the controls are within that form (and they all work when I come back to the page from elsewhere, just not on the initial landing).

Update: someone wanted some code, this is a minimum

<%@ Page Language="vb" AutoEventWireup="false" Inherits="Mysite._default" Codebehind="default.aspx.vb" %>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Title</title>
        <link href="Includes/Style.css" type="text/css" rel="stylesheet">
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:Button CssClass="Button" Id="cmdDone" Text="Done" runat="server" />
        </form>
    </body>
</html>

Partial Class _default
    Inherits Page

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack andalso request.HttpMethod = "Post" andalso ViewState("test") is nothing Then
            Throw new Exception("No, viewstate")
        End If

        ViewState("test") = true

    End Sub
   End Class

The problem is that when asp.net renders <form id="Form1" method="post" runat="server"> the resulting html is <form id="Form1" method="post" action="./"> and NOT <form id="Form1" method="post" action="./Default.aspx"> .

This results in the post going to the index and not to the page itself, and getting filtered out sometime before the page is loaded.

The solution is simple, fill out the action so that it goes directly to the page, action="Default.aspx".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM