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

属性值

包含控件标题的字符串。 默认值为空字符串 ("")。

示例

下面的代码示例演示如何以声明方式和编程方式使用 Title 属性。 示例的完整源代码位于类概述的 IWebPart “示例”部分。

代码示例的第一部分演示用户控件如何实现 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 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

// 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

适用于

另请参阅