共用方式為


WebPart.Subtitle 屬性

定義

取得與 Title 屬性值串連的字串,組成 WebPart 控制項的完整標題。

public:
 virtual property System::String ^ Subtitle { System::String ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual string Subtitle { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Subtitle : string
Public Overridable ReadOnly Property Subtitle As String

屬性值

字串,做為控制項的副標題。 預設值為空字串 ("")。

實作

屬性

範例

下列程式代碼範例示範如何為自定義 WebPart 控件的實例提供子標題。

此範例的第一個部分包含名為 TextDisplayWebPart之自定義控件的程序代碼。 此控件與類別概觀的範例區段中找到的 WebPart 自定義控件相同,不同之處在於它也會覆寫 Subtitle 屬性,以提供包含自定義控件實例虛構公司名稱的標準副標題。 若要執行程式碼範例,您必須編譯此原始程式碼。 您可以明確地編譯它,並將產生的元件放在網站的 Bin 資料夾或全域程式集緩存中。 或者,您可以將原始程式碼放在月臺的 App_Code資料夾中,其將在運行時間動態編譯。 此程式代碼範例假設您將原始程式碼編譯成元件、將它放在 Web 應用程式的 Bin 子資料夾中,並在網頁中使用 指示詞參考元件 Register 。 如需示範這兩種編譯方法的逐步解說,請參閱逐步解說 :開發和使用自定義 Web 伺服器控制件

using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, 
    Level=AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand, 
    Level=AspNetHostingPermissionLevel.Minimal)]
  public class TextDisplayWebPart : WebPart
  {
    private String _contentText = null;
    TextBox input;
    Label DisplayContent;
    const string _subTitle = "Contoso, Ltd";

    public TextDisplayWebPart()
    {
      this.AllowClose = false;
    }

    [Personalizable(), WebBrowsable]
    public String ContentText
    {
      get { return _contentText; }
      set { _contentText = value; }
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      DisplayContent.BackColor = 
        System.Drawing.Color.LightBlue;
      DisplayContent.Text = this.ContentText;
      this.Controls.Add(DisplayContent);
      input = new TextBox();
      this.Controls.Add(input);
      Button update = new Button();
      update.Text = "Set Label Content";
      update.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(update);
      ChildControlsCreated = true;
    }
    
    public override string Subtitle
    {
      get {return _subTitle; }
    }

    private void submit_Click(object sender, EventArgs e)
    {
      // Update the label string.
      if (!string.IsNullOrEmpty(input.Text))
      {
        _contentText = input.Text + @"<br />";
        input.Text = String.Empty;
        DisplayContent.Text = this.ContentText;
      }
    }
  }
}
Imports System.Security.Permissions 
Imports System.Web
Imports System.Web.UI.WebControls 
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

<AspNetHostingPermission(SecurityAction.Demand, _ 
  Level := AspNetHostingPermissionLevel.Minimal)> _ 
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
  Level := AspNetHostingPermissionLevel.Minimal)> _ 
Public Class TextDisplayWebPart 
  Inherits WebPart
  Private _contentText As String = Nothing
  Private input As TextBox
  Private DisplayContent As Label 
  Private Const _subTitle as String = "Contoso, Ltd"
  
  
  Public Sub New()  
    Me.AllowClose = False 
  End Sub 
  
  <Personalizable(), WebBrowsable()>  _ 
  Public Property ContentText() As String 
    Get 
      Return _contentText 
    End Get 
    Set 
      _contentText = value
    End Set 
  End Property
    
  Protected Overrides Sub CreateChildControls() 
    Controls.Clear()
    DisplayContent = New Label()
    DisplayContent.Text = Me.ContentText
    DisplayContent.BackColor = _
      System.Drawing.Color.LightBlue
    Me.Controls.Add(DisplayContent) 
    input = New TextBox() 
    Me.Controls.Add(input)
    Dim update As New Button()
    update.Text = "Set Label Content" 
    AddHandler update.Click, AddressOf Me.submit_Click
    Me.Controls.Add(update) 
    ChildControlsCreated = True 
  
  End Sub 
  
  Public Overrides ReadOnly Property Subtitle() As String
    Get
      Return _subTitle 
    End Get
  End Property

  Private Sub submit_Click(ByVal sender As Object, _
                           ByVal e As EventArgs)  
    ' Update the label string.
    If input.Text <> String.Empty Then
      _contentText = input.Text & "<br />"
      input.Text = String.Empty 
      DisplayContent.Text = Me.ContentText
    End If
  
  End Sub 
  
End Class 

End Namespace

範例的第二個部分是一個網頁,示範如何在 ASP.NET 網頁中參考 TextDisplayWebPart 控件。 在瀏覽器中載入頁面之後,控件的標題列文字會包含指派給宣告式標記中控件的標題、連字元分隔符,以及控件 TextDisplayWebPart 中自定義子標題的值。

<%@ page language="C#" %>
<%@ register tagprefix="aspSample" 
             Namespace="Samples.AspNet.CS.Controls" 
             Assembly="TextDisplayWebPartCS"%>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="Form1" runat="server">
      <asp:webpartmanager id="WebPartManager1" runat="server" />
    <asp:webpartzone
      id="WebPartZone1"
      runat="server"
      title="Zone 1"
      PartChromeType="TitleAndBorder">
        <parttitlestyle font-bold="true" ForeColor="#3300cc" />
        <partstyle
          borderwidth="1px"   
          borderstyle="Solid"  
          bordercolor="#81AAF2" />
        <zonetemplate>
          <aspSample:TextDisplayWebPart 
            runat="server"   
            id="textwebpart" 
            title = "Text WebPart" 
            />
          </zonetemplate>
    </asp:webpartzone>
  </form>
</body>
</html>
<%@ page language="VB" %>
<%@ register tagprefix="aspSample" 
             Namespace="Samples.AspNet.VB.Controls" 
             Assembly="TextDisplayWebPartVB"%>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="Form1" runat="server">
      <asp:webpartmanager id="WebPartManager1" runat="server" />
    <asp:webpartzone
      id="WebPartZone1"
      runat="server"
      title="Zone 1"
      PartChromeType="TitleAndBorder">
        <parttitlestyle font-bold="true" ForeColor="#3300cc" />
        <partstyle
          borderwidth="1px"   
          borderstyle="Solid"  
          bordercolor="#81AAF2" />
        <zonetemplate>
          <aspSample:TextDisplayWebPart 
            runat="server"   
            id="textwebpart" 
            title = "Text WebPart" 
            />
          </zonetemplate>
    </asp:webpartzone>
  </form>
</body>
</html>

備註

選擇性地使用 Subtitle 屬性,在自定義 WebPart 控件中傳回將附加至控件標題的標準子標題字串。

如果您在自定義WebPart控件中提供 屬性的值Subtitle,Web 元件控件會自動將它附加至 屬性的值Title,以建立控件的完整標題。

設定時,這個屬性的值可以使用設計工具自動儲存到資源檔。 如需詳細資訊,請參閱 LocalizableAttribute全球化和當地語系化

給繼承者的注意事項

若要提供自定義 WebPart 控件實例的子標題,您必須覆寫 Subtitle 屬性。

適用於

另請參閱