ASP.NET 的檢視狀態ViewState與Request.Form及string和String的區別

來源:互聯網
上載者:User

一:檢視狀態是 ASP.NET 頁架構預設情況下用於儲存往返過程之間的頁和控制項值的方法。當呈現頁的 HTML 形式時,需要在回傳過程中保留的頁的目前狀態和值將被序列化為 Base 64 編碼的字串,並輸出到檢視狀態的隱藏欄位中。通過實現自訂的 類以儲存頁資料,您可以更改預設行為並將檢視狀態儲存到另一個位置(如 SQL Server 資料庫)。ViewState 屬性是一個包含密鑰/值對(其中包含檢視狀態資料)的字典。 

       Web 應用程式是無狀態的。每次從伺服器請求網頁時,都會建立網頁類的一個新執行個體。這通常意味著在每次往返過程中將會丟失所有與該頁面及其控制項關聯的資訊。例如,如果使用者將資訊輸入到 HTML 網頁上的文字框中,此資訊將發送到伺服器,但是不會返回到用戶端。為了克服 Web 編程的這一固有局限性,ASP.NET 頁架構套件含幾種狀態管理功能,可以將往返過程之間的頁和控制項值儲存到 Web 服務器,其中一種功能便是檢視狀態。 

 

   可以將下列類型的Object Storage Service到檢視狀態中:

  • 字串

  • 整數

  • 布爾值

  • Array 對象

  • ArrayList 對象

  • 雜湊表

  • 自訂類型轉換器 

      ViewState屬性用法如下:

        ViewState["paramTable"] = (Hashtable)Session["paramTable"];
        ViewState.Add("paramTable", (Hashtable)Session["paramTable"]);

二:  在CS檔案中獲得對應頁面中的下拉框DropDownList_sitebranch值可以有以下幾種方法獲得:

        siteInfo.FZJGID = DropDownList_sitebranch.SelectedItem.Value.Trim();
        siteInfo.FZJGID = Request.Form["DropDownList_sitebranch"].ToString();
        siteInfo.FZJGID = Request["DropDownList_sitebranch"].ToString();

   其中Request.Form是 ASP.NET 擷取表單變數的集合。

三:在C#中,string和String有什麼區別?

  string屬於C#的類型  
  String屬於.NET   的類型  
   兩者可以等同使用,推薦用string 
  在.NET   所有的類型都是對象。下面是.NET   C#   VB.NET   IL的類型對應表:  
   
  NET                             C#               VB.NET                 IL           值或引用  
  System.Boolean     bool             Boolean           bool                 Value  
  System.Byte           byte             Byte         unsigned   int8       Value  
  System.Char           char             Char                 char                 Value  
  System.DateTime     -                   Date                 -                           Value  
  System.Decimal   decimal         Decimal           -                           Value  
  System.Double       double         Double           float64             Value  
  System.Int16         short           Short             int16                 Value  
  System.Int32         int               Integer         int32                 Value  
  System.Int64         long               Long             int64                 Value  
  System.Object       object         Object           object           Reference  
  System.SByte           sbyte           -                       int8                   Value  
  System.Single       float           Single             float32             Value  
  System.String         string         String           string           Reference  
  System.UInt16         ushort         -             unsigned   int16         Value  
  System.UInt32         uint             -             unsigned   int32           Value  
  System.UInt64         ulong         -               unsigned   int64           Value  
   
  從上表可以看到,不僅僅string和String,還有許多類似的類型。有的沒有對應的類型,這時使用的就是.NET的,比如說在C#中沒有date類型,所以在C#中我們使用.NET   的Date.   這也是.NET的跨語言特性之一。  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.