zoukankan      html  css  js  c++  java
  • 如何:引用 ASP.NET 母版页的内容 (zz)

    原文地址:http://msdn2.microsoft.com/zh-cn/library/xxwa0ff0(VS.80).aspx
    ASP.NET 
    如何:引用 ASP.NET 母版页的内容 

    可以在内容页中编写代码来引用母版页中的属性、方法和控件,但这种引用有一定的限制。对于属性和方法的规则是:如果它们在母版页上被声明为公共成员,则可以引用它们。这包括公共属性和公共方法。在引用母版页上的控件时,没有只能引用公共成员的这种限制。

    引用母版页上的公共成员

    1. 在内容页中添加 @ MasterType 指令。在该指令中,将 VirtualPath 属性设置为母版页的位置,如下面的示例所示:

      <%@ MasterType virtualpath="~/Masters/Master1.master" %>

      此指令使内容页的 Master 属性被强类型化。

    2. 编写代码,将母版页的公共成员用作 Master 属性的一个成员,如本例中,将母版页名为 CompanyName 的公共属性的值赋给内容页上的一个文本框:

    引用母版页上的控件

    • 使用 FindControl 方法,将 Master 属性的返回值用作命名容器。

      下面的代码示例演示如何使用 FindControl 方法获取对母版页上的两个控件的引用(一个 TextBox 控件和一个 Label 控件)。因为 TextBox 控件处在 ContentPlaceHolder 控件的内部,必须首先获取对 ContentPlaceHolder 的引用,然后使用其 FindControl 方法来定位 TextBox 控件。

      Visual Basic
      Sub Page_Load()
          Dim mpContentPlaceHolder As ContentPlaceHolder
          Dim mpTextBox As TextBox
          mpContentPlaceHolder = _
          CType(Master.FindControl("ContentPlaceHolder1"), _
          ContentPlaceHolder)
          If Not mpContentPlaceHolder Is Nothing Then
          mpTextBox = CType(mpContentPlaceHolder. _
          FindControl("TextBox1"), TextBox)
          If Not mpTextBox Is Nothing Then
          mpTextBox.Text = "TextBox found!"
          End If
          End If
          ' Gets a reference to a Label control not in a 
          ' ContentPlaceHolder
          Dim mpLabel As Label
          mpLabel = CType(Master.FindControl("masterPageLabel"), Label)
          If Not mpLabel Is Nothing Then
          Label1.Text = "Master page label = " + mpLabel.Text
          End If
          End Sub
          
      void Page_Load()
          {
          // Gets a reference to a TextBox control inside 
          // a ContentPlaceHolder
          ContentPlaceHolder mpContentPlaceHolder;
          TextBox mpTextBox;
          mpContentPlaceHolder =
          (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
          if(mpContentPlaceHolder != null)
          {
          mpTextBox =
          (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
          if(mpTextBox != null)
          {
          mpTextBox.Text = "TextBox found!";
          }
          }
          // Gets a reference to a Label control that not in 
          // a ContentPlaceHolder
          Label mpLabel = (Label) Master.FindControl("masterPageLabel");
          if(mpLabel != null)
          {
          Label1.Text = "Master page label = " + mpLabel.Text;
          }
          }
          

    请参见

  • 相关阅读:
    Linux编译安装中configure、make和make install各自的作用
    转载的 Linux下chkconfig命令详解
    MYSQL主从不同步延迟原理分析及解决方案(摘自http://www.jb51.net/article/41545.htm)
    mysql主从延迟(摘自http://www.linuxidc.com/Linux/2012-02/53995.htm)
    http://ninghao.net/video/1554不错的学习网址
    javascript 内置对象和方法
    javascript 函数
    javascript 基础
    css z-index
    css 透明度
  • 原文地址:https://www.cnblogs.com/strinkbug/p/590133.html
Copyright © 2011-2022 走看看