TitledText Box : Extends CompositeControl « Custom Controls « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TitledTextBoxTest" %>
<%@ Register TagPrefix="apress" Namespace="CustomServerControlsLibrary"
  Assembly="CustomServerControls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <apress:TitledTextBox Title="Sample Title" ID="TitledTextBox1" runat="server" BackColor="#FFFF80" Font-Size="X-Large" Height="85px" Text="Sample Text" Width="569px" />
    </div>
    </form>
</body>
</html>

File: CustomServerControls.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CustomServerControlsLibrary
{
  [Designer(typeof(TitledTextBoxDesigner))]
  public class TitledTextBox : CompositeControl
  {
    public TitledTextBox() : base()
    {
      Title = "";
      Text = "";
    }
    protected Label label;
    protected TextBox textBox;
    
    public string Title
    {
      get {return (string)ViewState["Title"];}
      set {ViewState["Title"] = value;}
    }

    public string Text
    {
      get {return (string)ViewState["Text"];}
      set {ViewState["Text"] = value;}
    }

    protected override void CreateChildControls()
    {
      label = new Label();
      label.EnableViewState = false;
      label.Text = Title;
      Controls.Add(label);

      Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

      textBox = new TextBox();
      textBox.EnableViewState = false;
      textBox.Text = Text;
      textBox.TextChanged += new EventHandler(OnTextChanged);
      Controls.Add(textBox);
    }

    public event EventHandler TextChanged;
    
    protected virtual void OnTextChanged(object sender, EventArgs e)
    {
      if (TextChanged != null)
        TextChanged(this, e);
    }
  }
}

public partial class TitledTextBoxTest : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
}








14.21.Extends CompositeControl
14.21.1.Creating a composite control (C#)
14.21.2.Creating a composite control (VB)
14.21.3.Exposing control properties in a composite control (C#)
14.21.4.Exposing control properties in a composite control (VB)
14.21.5.TitledText Box