WebPart.Subtitle Property

Definition

Gets a string that is concatenated with the Title property value to form a complete title for a WebPart control.

C#
[System.ComponentModel.Browsable(false)]
public virtual string Subtitle { get; }

Property Value

A string that serves as a subtitle for the control. The default value is an empty string ("").

Implements

Attributes

Examples

The following code example demonstrates how to provide a subtitle for instances of a custom WebPart control.

The first part of this example contains the code for a custom control named TextDisplayWebPart. This control is the same as the custom control which is found in the Example section of the WebPart class overview, except that it also overrides the Subtitle property to provide a standard subtitle containing a fictitious company name for instances of the custom control. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This code example assumes that you compile the source code into an assembly, place it in a Bin subfolder of your Web application, and reference the assembly with a Register directive in your Web page. For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Web Server Control.

C#
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;
      }
    }
  }
}

The second part of the example is a Web page that shows how to reference the TextDisplayWebPart control in an ASP.NET Web page. After you load the page in a browser, the control's title bar text includes the title assigned to the control in the declarative markup, a hyphen separator, and the value of the custom subtitle from the TextDisplayWebPart control.

ASP.NET (C#)
<%@ 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>

Remarks

Use the Subtitle property optionally to return a standard subtitle string in your custom WebPart control that will be appended to the control's title.

If you provide a value for the Subtitle property in a custom WebPart control, the Web Parts control set automatically appends it to the value of the Title property to create a complete title for the control.

The value of this property, when set, can be saved automatically to a resource file by using a designer tool. For more information, see LocalizableAttribute and Globalization and Localization.

Notes to Inheritors

To supply a subtitle for instances of a custom WebPart control, you must override the Subtitle property.

Applies to

Proizvod Verzije
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also