惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

博客园 - yi

Rabbitmq的使用及Web监控工具使用 Fiddler的配置 轻松实现SQL Server与Access、Excel数据表间的导入导出 Sql server性能优化白皮书 索引的优化脚本 查找当前的登录用户 usp_who5脚本,查找当前的进程 linq to sql的性能和reader相比只是差一点点吗 锁与索引 一个数据访问层的小工具 转:对XML插入操作 - yi - 博客园 查看当前的连接和锁 对数据的分页再一次思考 关注商业价值 应用程序优化 关于异常处理的一点看法 骂的人多了,也成了真理 样式小记 - yi - 博客园 重命名你的数据库
不浪费自己的时间,同时也不浪费别人的时间 - yi - 博客园
yi · 2008-03-30 · via 博客园 - yi

    或许我很少在这里写东西,因为我要写一些对大家有用的东西,不想浪费自己写的时间,更不想浪费别人看的时间。

今天看了一编帖子,大概内容如下:

<%@ 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"  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)

说的是,获取母亲的属性,这里我想说的是,是先有父母,还是先有儿子,如果先有父母,何谈变化,如果没有变化,方法二三四的执行效率是那一个高呢,这个大家应该很清楚。
是不是啥东西都需要变化呢,如果需要应对变化,省级、市级、县级的数据库难道就要设计很多张表吗?一些理论的东西只适应一定的范围,如果超出了这个范围就无从谈起了