简体   繁体   English

default.aspx 的视图状态未加载/缺失

[英]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.我的默认页面在执行回发时不包括视图状态,即视图状态丢失,当我在调试期间查看它们时,它没有包含在 forms 或参数 collections 中。 This also causes the IsPostBack property to show as False and the events associated with the postback to not be called.这也会导致 IsPostBack 属性显示为 False,并且不会调用与回发关联的事件。

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"> .问题是,当 asp.net 呈现<form id="Form1" method="post" runat="server">时,生成的 html 是<form id="Form1" method="post" action="./">而不是<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".解决方法很简单,填写action让它直接跳转到页面,action="Default.aspx"。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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