现在的位置: 首页 > 综合 > 正文

ASP.NET 中母版页的传值(ViewState版)(从我的老博客中转过来的(http://kenne.cnblogs.com))

2013年01月17日 ⁄ 综合 ⁄ 共 2770字 ⁄ 字号 评论关闭

      虽然母版页这个功能出来很久了,但是一直都没有机会去使用它。
      最近我计划把公司网站升级到.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)
虽然没试过,但是我想绝对可以,因为母版页和内容页是一个合并过程,先调用母版页在调用内容页,只要根据这个执行顺序去做,完全可以实现。希望哪位时间比较空余的人在此贴后面贴上实现代码,以供大家分享。

抱歉!评论已关闭.