Asp.Net基礎 – 4.ASP.Net揭秘之Input版自增 + 5.ViewState初探

來源:互聯網
上載者:User
文章目錄
  • 4.2.把IntValue1.html設為起始頁

4.ASP.Net揭秘之Input版自增

  4.2.思考:把IntValue1.html設為起始頁

5.ViewState初探

  5.1.只有設定了name的input、textarea、select的value屬性值才會被提交給伺服器

  5.2.禁用ViewState的方法

4.ASP.Net揭秘之Input版自增
4.1.實現input的自增:

點擊按鈕input中的值自動增加,代碼如下 value=”@value”中@value為自訂

IntValue1.html代碼

<form action="IntValue1.ashx">        <input name="ispostback" type="hidden" value="true"/>        <input name="number" type="text" value="@value"/><input type="submit" value="自增"/>    </form>

IntValue1.ashx代碼

public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "text/html";        string ispostBack = context.Request["ispostback"];        string number = context.Request["number"];        if (ispostBack == "true")//說明點擊【自增】進來的,需要把當前數值自增        {            int i = Convert.ToInt32(number);            i++;            number = i.ToString();        }        else//第一次進來        {            number = "0";        }        string fullpath = context.Server.MapPath("IntValue1.html");        string content = System.IO.File.ReadAllText(fullpath);        content = content.Replace("@value", number);        context.Response.Write(content);    }

提交3次後

思考:4.2.把IntValue1.html設為起始頁

 應該設定ashx .

5.ViewState初探
5.1.只有設定了name的input、textarea、select的value屬性值才會被提交給伺服器
為什麼單使用div在伺服器取不出來值呢?因為不是伺服器來讀取客戶的網頁,而是瀏覽器收集客戶再表單中輸入的欄位,然後形成請求參數發給伺服器處理常式,由於沒有把div當前的innerText發給伺服器,所以伺服器無法得知當前的值。也不要幻想有辦法能將div的innerText提交給伺服器,因為只有設定了name的input、textarea、select的value屬性值才會被提交給伺服器。
<!--這種不行start--><div name="aaa">@text</div> <!--這種不行end--><!-- 用hidden來傳strat--><input name="aaa" type="hidden" value="@text"/> <div>@text</div><!-- 用hidden來傳end-->

寫一個自增的頁面

自增.aspx

<body>    <form id="form1" runat="server">    <div>    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />    </div>    </form></body>

自增.aspx.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class 寬度自增 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {             Label1.Text = "0";        }       }    protected void Button1_Click(object sender, EventArgs e)    {        int i = Convert.ToInt32(Label1.Text);        i++;        Label1.Text = i.ToString();    }}

查看原始碼,圖為ViewState的值

上面value=”/wEPDwUj…”為ViewState的值。

再來看input自增,假如用一個textbox控制項來取代Lable控制項,(代碼就不寫了,基本一樣)結果,viewstate裡的值沒有儲存textbox的值,得出下面

結論

Label版本的值存到了ViewState中,TextBox版本的不用存,因為TextBox就是input,自己就會提交給伺服器,不需要隱藏欄位

(查看ViewState的值可以用一個ViewState的小工具: ViewStateDecoder2)

5.2禁用ViewState的方法(viewstate裡不存值)

Label反編譯的結果,讀取的是ViewState

禁用單個控制項的ViewState設定enableviewstate=false,

禁用ViewState以後TextBox版本不受影響,Div版本受影響,因為input的value不依靠ViewState。禁用整個頁面的,在aspx的Page指令區加上EnableViewState="false" 。


內網系統、互連網的後台可以盡情的用ViewState。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.