ASP.NET Tutorial/WebPart/WebPart — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:30, 26 мая 2010
Creating a custom Web Part control (C#)
<source lang="csharp">
using System; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
public class StateListBox : WebPart {
private String _LabelStartText = " Enter State Name: "; TextBox StateInput; ListBox StateContents; public StateListBox() { this.AllowClose = false; } [Personalizable(), WebBrowsable] public String LabelStartText { get { return _LabelStartText; } set { _LabelStartText = value; } } protected override void CreateChildControls() { Controls.Clear(); Label InstructionText = new Label(); InstructionText.BackColor = System.Drawing.Color.LightGray; InstructionText.Font.Name = "Verdana"; InstructionText.Font.Size = 10; InstructionText.Font.Bold = true; InstructionText.Text = LabelStartText; this.Controls.Add(InstructionText); Literal LineBreak = new Literal(); LineBreak.Text = "
"; this.Controls.Add(LineBreak); this.Controls.Add(StateInput); Button InputButton = new Button(); InputButton.Text = "Input State"; InputButton.Click += new EventHandler(this.Button1_Click); this.Controls.Add(InputButton); Literal Spacer = new Literal(); Spacer.Text = "<paragraph>"; this.Controls.Add(Spacer); this.Controls.Add(StateContents); ChildControlsCreated = true; } private void Button1_Click(object sender, EventArgs e) { StateContents.Items.Add(StateInput.Text); StateInput.Text = String.Empty; StateInput.Focus(); }
}</source>
Creating a custom Web Part control (VB)
<source lang="csharp">
Imports System Imports System.Web Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts
Public Class StateListBox
Inherits WebPart Private _LabelStartText As String = " Enter State Name: " Dim StateInput As New TextBox Dim StateContents As New ListBox Public Sub New() Me.AllowClose = False End Sub <Personalizable(), WebBrowsable()> _ Public Property LabelStartText() As String Get Return _LabelStartText End Get Set(ByVal value As String) _LabelStartText = value End Set End Property Protected Overrides Sub CreateChildControls() Controls.Clear() Dim InstructionText As New Label InstructionText.BackColor = Drawing.Color.LightGray InstructionText.Font.Name = "Verdana" InstructionText.Font.Size = 10 InstructionText.Font.Bold = True InstructionText.Text = LabelStartText Me.Controls.Add(InstructionText) Dim LineBreak As New Literal LineBreak.Text = "
" Me.Controls.Add(LineBreak) Me.Controls.Add(StateInput) Dim InputButton As New Button InputButton.Text = "Input State" AddHandler InputButton.Click, AddressOf Me.Button1_Click Me.Controls.Add(InputButton) Dim Spacer As New Literal Spacer.Text = "<paragraph>" Me.Controls.Add(Spacer) Me.Controls.Add(StateContents) ChildControlsCreated = True End Sub Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) StateContents.Items.Add(StateInput.Text) StateInput.Text = String.Empty StateInput.Focus() End Sub
End Class</source>