程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Web服務器控件:PlaceHolder控件

Web服務器控件:PlaceHolder控件

編輯:關於.NET

定義和用法

PlaceHolder 控件用於為代碼添加的控件預留空間。在頁控件層次結構中為以編程方式添加的控件保留位置。

注釋:PlaceHolder 不會產生任何可見的輸出(僅僅是網頁上其它控件的容器)。

屬性

屬性 描述 .Net runat 規定控件是服務器。必須設置為 "server"。 1.0

Web 控件標准屬性

AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, 
CSSClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, 
SkinID, Style, TabIndex, ToolTip, Width

控件標准屬性

AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, 
EnableTheming, EnableVIEwState, ID, NamingContainer, Page, Parent, Site, 
TemplateControl, TemplateSourceDirectory, UniqueID, Visible

語法:

<ASP:PlaceHolder
    EnableTheming="True|False"
    EnableVIEwState="True|False"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Visible="True|False"
/>

備注:將 PlaceHolder 控件用作容器,從而將動態添加的服務器控件存儲到網頁中。PlaceHolder 控件不產生任何可見的輸出,僅用作網頁上其他控件的容器。您可以使用 Control.Controls 集合在 PlaceHolder 控件中添加、插入或移除控件。

實例:

下面的示例演示如何將 Web 服務器控件添加到 PlaceHolder 控件中。

Visual Basic

<%@ Page Language="VB" AutoEventWireup="True" %>
<Html>
<head>
   <script runat="server">
      Sub Page_Load(Sender As Object, e As EventArgs)
         Dim myButton As HtmlButton = New HtmlButton()
         myButton.InnerText = "Button 1"
         PlaceHolder1.Controls.Add(myButton)
         myButton = New HtmlButton()
         myButton.InnerText = "Button 2"
         PlaceHolder1.Controls.Add(myButton)
         myButton = New HtmlButton()
         myButton.InnerText = "Button 3"
         PlaceHolder1.Controls.Add(myButton)
         myButton = New HtmlButton()
         myButton.InnerText = "Button 4"
         PlaceHolder1.Controls.Add(myButton)
      End Sub
   </script>
</head>
<body>
   <form runat="server">
      <h3>PlaceHolder Example</h3>
      <ASP:PlaceHolder id="PlaceHolder1" 
           runat="server"/>
   </form>
</body>
</Html>

C#

<%@ Page Language="C#" AutoEventWireup="True" %>
<Html>
<head>
   <script runat="server">
      void Page_Load(Object sender, EventArgs e)
      {
         HtmlButton myButton = new HtmlButton();
         myButton.InnerText = "Button 1";
         PlaceHolder1.Controls.Add(myButton);
         myButton = new HtmlButton();
         myButton.InnerText = "Button 2";
         PlaceHolder1.Controls.Add(myButton);
         myButton = new HtmlButton();
         myButton.InnerText = "Button 3";
         PlaceHolder1.Controls.Add(myButton);
         myButton = new HtmlButton();
         myButton.InnerText = "Button 4";
         PlaceHolder1.Controls.Add(myButton);
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>PlaceHolder Example</h3>
      <ASP:PlaceHolder id="PlaceHolder1" 
           runat="server"/>
   </form>
</body>
</Html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved