zoukankan      html  css  js  c++  java
  • 通过在 Web 表单中维持对象的 ViewState (视图状态)

    通过在 Web 表单中维持对象的 ViewState (视图状态),您可以省去大量的编码工作。

    维持 ViewState (视图状态)

    当 classic ASP中的表单被提交时,所有的表单值都会被清空。设想一下,您提交了一张带有大量信息的表单,而服务器返回了一个错误。您将不得不返回表单,然后更正其中的信息。您点击后退按钮,然后会发生什么呢... 所有的表单值都被清空了,而您将不得不重新开始所有的一切。站点不会维持您的 ViewState。

    当 ASP .NET 中的表单被提交时,表单会随所有表单值一同重新出现。如何做到的呢?这是由于 ASP .NET 维持了您的ViewState。ViewState 会在页面被提交到服务器时指示其状态。通过在每张页面中的一个 <formrunat="server"> 控件中放置一个隐藏域,我们就可以定义页面的状态了。源代码可能类似这样:

    <form name="_ctl0" method="post" action="page.aspx" id="_ctl0">
    <input type="hidden" name="__VIEWSTATE"
    value="dDwtNTI0ODU5MDE1Ozs+ZBCF2ryjMpeVgUrY2eTj79HNl4Q=" />

    .....some code

    </form>

    维持 ViewState 是 ASP.NET Web 表单的默认设置。如果您不希望维持 ViewState,请在 .aspx页面的顶部包含指令: <%@ Page EnableViewState="false"%>,或为任意控件添加属性:EnableViewState="false"。

    请看下面的 .aspx 文件。它演示了老的运行方式。当您单击提交按钮时,表单值就会消失:

    <html>
    <body>

    <form action="demo_classicasp.aspx" method="post">
    Your name: <input type="text" name="fname" size="20">
    <input type="submit" value="Submit">
    </form>
    <%
    dim fname
    fname=Request.Form("fname")
    If fname<>"" Then
    Response.Write("Hello " & fname & "!")
    End If
    %>

    </body>
    </html>

    TIY

    这是新的 ASP .NET 方式。当您点击提交按钮时,表单值不会消失:

    <script runat="server">
    Sub submit(sender As Object, e As EventArgs)
    lbl1.Text="Hello " & txt1.Text & "!"
    End Sub
    </script>

    <html>
    <body>

    <form runat="server">
    Your name: <asp:TextBox id="txt1" runat="server" />
    <asp:Button OnClick="submit" Text="Submit" runat="server" />
    <p><asp:Label id="lbl1" runat="server" /></p>
    </form>

    </body>
    </html>

  • 相关阅读:
    Codeforces Round #632 (Div. 2)
    Codeforces Round #630 (Div. 2)
    多项式全家桶
    Educational Codeforces Round 84 (Rated for Div. 2)
    【cf1186E】E. Vus the Cossack and a Field(找规律+递归)
    [CF847B] Preparing for Merge Sort
    [CF858D] Polycarp's phone book
    [CF911D] Inversion Counting
    [CF938C] Constructing Tests
    [CF960C] Subsequence Counting
  • 原文地址:https://www.cnblogs.com/lvcha/p/1683083.html
Copyright © 2011-2022 走看看