IWebPart.Title 屬性

定義

取得或設定 WebPart 控制項的標題。

public:
 property System::String ^ Title { System::String ^ get(); void set(System::String ^ value); };
public string Title { get; set; }
member this.Title : string with get, set
Public Property Title As String

屬性值

String

包含控制項標題的字串。 預設值為空字串 ("")。

範例

下列程式碼範例示範 屬性的 Title 宣告式和程式設計用法。 此範例的完整原始程式碼位於類別概觀的 IWebPart Example 區段中。

程式碼範例的第一個部分示範使用者控制項如何實作 Title 屬性。

public string Title
{
  get
  {
    object objTitle = ViewState["Title"];
    if (objTitle == null)
      return String.Empty;

    return (string)objTitle;
  }
  set
  {
    ViewState["Title"] = value;
  }
}
Public Property Title() As String _
  Implements IWebPart.Title
  Get
    Dim objTitle As Object = ViewState("Title")
    If objTitle Is Nothing Then
      Return String.Empty
    End If
    Return CStr(objTitle)
  End Get
  Set(ByVal value As String)
    ViewState("Title") = value
  End Set
End Property

程式碼範例的第二個部分示範使用者控制項中的 方法,當使用者從頁面上的選項按鈕選取適當的屬性名稱、在文字方塊中設定新的值,然後按一下 [更新] 按鈕時,以程式設計方式設定屬性的值 Title

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

// Update the selected IWebPart property value.
void Button1_Click(object sender, EventArgs e)
{
  String propertyValue = Server.HtmlEncode(TextBox3.Text);
  TextBox3.Text = String.Empty;

  switch (RadioButtonList1.SelectedValue)
  {
    case "title":
      this.Title = propertyValue;
      break;
    case "description":
      this.Description = propertyValue;
      break;
    case "catalogiconimageurl":
      this.CatalogIconImageUrl = propertyValue;
      break;
    case "titleiconimageurl":
      this.TitleIconImageUrl = propertyValue;
      break;
    case "titleurl":
      this.TitleUrl = propertyValue;
      break;
    default:
      break;
  }
}
' Update the selected IWebPart property value.
Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
  Dim propertyValue As String = Server.HtmlEncode(TextBox3.Text)
  TextBox3.Text = String.Empty
    
  Select Case RadioButtonList1.SelectedValue
    Case "title"
      Me.Title = propertyValue
    Case "description"
      Me.Description = propertyValue
    Case "catalogiconimageurl"
      Me.CatalogIconImageUrl = propertyValue
    Case "titleiconimageurl"
      Me.TitleIconImageUrl = propertyValue
    Case "titleurl"
      Me.TitleUrl = propertyValue
    Case Else
  End Select

End Sub 'Button1_Click

程式碼範例的第三個部分示範如何在 控制項中 WebPartZone 參考實 IWebPart 作 介面的使用者控制項,以及如何 Title 以宣告方式在 控制項上設定 屬性。

<%@ page language="c#" %>
<%@ register tagprefix="uc1" 
    tagname="AccountUserControlCS" 
    src="AccountUserControlcs.ascx"%>
<!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>
      Personalizable User Control with IWebPart Properties
    </title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:webpartmanager id="WebPartManager1" runat="server" />
      <asp:webpartzone 
        id="zone1" 
        runat="server" 
        headertext="Main" 
        CloseVerb-Enabled="false">
        <zonetemplate>
          <uc1:AccountUserControlCS 
            runat="server" 
            id="accountwebpart" 
            title="Account Form"
            Description="Account Form with default values."
            CatalogIconImageUrl="MyCatalogIcon.gif"
            TitleIconImageUrl="MyTitleIcon.gif"
            TitleUrl="MyUrl.html"/>
        </zonetemplate>
      </asp:webpartzone>    
    </form>
  </body>
</html>
<%@ page language="VB" %>
<%@ register tagprefix="uc1" 
    tagname="AccountUserControlVB" 
    src="AccountUserControlvb.ascx"%>
<!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>
      Personalizable User Control with IWebPart Properties
    </title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:webpartmanager id="WebPartManager1" runat="server" />
      <asp:webpartzone 
        id="zone1" 
        runat="server" 
        headertext="Main" 
        CloseVerb-Enabled="false">
        <zonetemplate>
          <uc1:AccountUserControlVB 
            runat="server" 
            id="accountwebpart" 
            title="Account Form"
            Description="Account Form with default values."
            CatalogIconImageUrl="MyCatalogIcon.gif"
            TitleIconImageUrl="MyTitleIcon.gif"
            TitleUrl="MyUrl.html"/>
        </zonetemplate>
      </asp:webpartzone>    
    </form>
  </body>
</html>

備註

控制項標題列中可見的標題文字是由 Title 屬性所設定。

如果您未提供控制項的標題,Web 組件控制項集會自動計算要當做標題使用的預設字串。 如需詳細資訊,請參閱DisplayTitle。 此外,您也可以提供附加至標題字串的預設子標題。 如需詳細資訊,請參閱 Subtitle

適用於

另請參閱