ASP.NET Tutorial/Custom Controls/Extends CompositeControl
Содержание
Creating a composite control (C#)
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl2 runat=server></{0}:WebCustomControl2>")]
public class WebCustomControl2 : CompositeControl
{
protected TextBox textbox = new TextBox();
protected override void CreateChildControls()
{
this.Controls.Add(textbox);
}
}
}
Creating a composite control (VB)
Imports System.ruponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<DefaultProperty("Text")> _
<ToolboxData("<{0}:[WebCustomControl2] runat=server></{0}:[WebCustomControl2]>")> _
Public Class [WebCustomControl2]
Inherits System.Web.UI.WebControls.rupositeControl
Protected textbox As TextBox
Protected Overrides Sub CreateChildControls()
Me.Controls.Add(textbox)
End Sub
End Class
Exposing control properties in a composite control (C#)
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl2 runat=server></{0}:WebCustomControl2>")]
public class WebCustomControl2 : CompositeControl
{
protected TextBox textbox = new TextBox();
public string Text
{
get
{
EnsureChildControls();
return textbox.Text;
}
set
{
EnsureChildControls();
textbox.Text = value;
}
}
protected override void CreateChildControls()
{
this.Controls.Add(textbox);
}
}
}
Exposing control properties in a composite control (VB)
Imports System.ruponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<DefaultProperty("Text")> _
<ToolboxData("<{0}:[WebCustomControl2] runat=server></{0}:[WebCustomControl2]>")> _
Public Class [WebCustomControl2]
Inherits System.Web.UI.WebControls.rupositeControl
Protected textbox As TextBox
Public Property Text() As String
Get
EnsureChildControls()
Return textbox.Text
End Get
Set(ByVal value As String)
EnsureChildControls()
textbox.Text = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Me.Controls.Add(textbox)
End Sub
End Class
TitledText Box
<%@ 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.ruponentModel;
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(" "));
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)
{
}
}