ASP.NET 中主版頁面的傳值(ViewState版)

來源:互聯網
上載者:User
雖然主版頁面這個功能出來很久了,但是一直都沒有機會去使用它。
      最近我計劃把公司網站升級到.NET1.1 升級到 .NET 3.5上去,除了隱式類型、擴充方法、Lambda運算式、LinQ等新特性外,主版頁面這個功能頁是肯定會去玩玩的。於是今天用VS2008建立一個網站,嘗試著進行一些最基本的任務,資料繫結,頁面傳值等。
這隻是第一天,由於工作的時候不斷的有外來任務打擾,所以學習成果甚少,除了看了MSDN一些資料,也查了一下互連網上的資料,在CSDN中有一個文章裡說,主版頁面不能通過ViewState傳值,其實這是錯誤的。主版頁面絕對可以通過ViewState來傳值,並且方法非常簡單。
      下面為主版頁面的代碼:
 

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="Main" MasterPageFile="" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>主主版頁面</title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <div style="border-style: dashed; font-family: 宋體, Arial, Helvetica, sans-serif; font-size: xx-large; font-weight: 100; font-style: normal; font-variant: normal; text-transform: capitalize; color: #FF0000">主要的主版頁面!</div>
      <br />
      <div align="center"
                style="font-family: 宋體, Arial, Helvetica, sans-serif; font-size: large; font-weight: 400; font-style: italic">Hi!歡迎您使用ASP.NET3.5!<br />
          <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            </div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
       
        </asp:ContentPlaceHolder>
        <div align="center" style="border-style: ridge">著作權,違者必究! 浙江新能量科技有限公司2008</div>
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class Main : System.Web.UI.MasterPage,IMasterData
{
    public int BtnCount
    {
        get
        {
            return this.ViewState["BtnCount"] == null ? 0 : Convert.ToInt32(this.ViewState["BtnCount"]);
        }
        set
        {
            this.ViewState["BtnCount"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.BtnCount++;
    }
}

其中IMasterData
介面的代碼如下:
/// <summary>
///主版頁面的資料
/// </summary>
public interface IMasterData
{
    int BtnCount { get; set; }
}

方法一:
在內容頁面中使用:
((Main)this.Page.Master).BtnCount;

此方法不推薦,靈活程度很低,萬一哪天BtnCount或者Main更改了,或者是在多個主版頁面之中選擇,這行代碼就很危險。

方法二:
在內容頁面中使用:
((IMasterData)this.Page.Master).BtnCount;

推薦,定義一個介面,通過介面來調用要調用的成員,針對介面編程,好處不用我說了。

方法三:
在內容頁面中使用:
this.Page.Master.GetType().GetProperty("BtnCount").GetValue(this.Page.Master, null)

這是一種相對靈活的方法,在編譯的時候無法判斷錯誤的方法,特殊情況下可以考慮使用此方法。

方法四:
override object SaveViewState()和override void LoadViewState(object savedState)

聯繫我們

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