WebPart コンストラクタ
アセンブリ: System.Web (system.web.dll 内)


WebPart コンストラクタは、コントロールの機能を決定するさまざまな Allow プロパティを含むいくつかの UI 指向のプロパティの既定値を初期化します。この後、これらのプロパティは継承クラス インスタンスに取り込まれます。

次のコード例では、WebPart クラスから派生するコントロールは、基本 WebPart コンストラクタで設定された既定のプロパティ値を継承しますが、その後、派生コントロールのコンストラクタの 1 つのプロパティの値を変更します。
この例の最初の部分には、TextDisplayWebPart という名前のカスタム WebPart コントロールのコードが含まれています。このコントロールは、Web パーツ コントロール セットの機能を利用できるようにする単純なカスタム WebPart コントロールを作成する方法を示します。カスタム コントロールのコンストラクタでは、TextDisplayWebPart.AllowClose プロパティが false に設定されます。これによって、ユーザーは Web ページでコントロールを終了できなくなります。コード例を実行するためには、このソース コードをコンパイルする必要があります。それを明示的にコンパイルし、コンパイル済みのアセンブリを Web サイトの Bin フォルダまたはグローバル アセンブリ キャッシュに配置できます。サイトの App_Code フォルダにソース コードを配置し、実行時に動的にコンパイルすることもできます。このコード例は、ソース コードをアセンブリにコンパイルし、それを各自の Web アプリケーションの Bin サブフォルダに配置し、アセンブリを各自の Web ページの Register ディレクティブで参照することを前提にしています。両方のコンパイル方法を示すチュートリアルについては、「チュートリアル : カスタム サーバー コントロールの開発と使用」を参照してください。
Imports System Imports System.Security.Permissions Imports System.Web Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Namespace Samples.AspNet.VB.Controls <AspNetHostingPermission(SecurityAction.Demand, _ Level := AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermission(SecurityAction.InheritanceDemand, _ Level := AspNetHostingPermissionLevel.Minimal)> _ Public Class TextDisplayWebPart Inherits WebPart Private _contentText As String = Nothing Private input As TextBox Private DisplayContent As Label Public Sub New() Me.AllowClose = False End Sub <Personalizable(), WebBrowsable()> _ Public Property ContentText() As String Get Return _contentText End Get Set _contentText = value End Set End Property Protected Overrides Sub CreateChildControls() Controls.Clear() DisplayContent = New Label() DisplayContent.Text = Me.ContentText DisplayContent.BackColor = _ System.Drawing.Color.LightBlue Me.Controls.Add(DisplayContent) input = New TextBox() Me.Controls.Add(input) Dim update As New Button() update.Text = "Set Label Content" AddHandler update.Click, AddressOf Me.submit_Click Me.Controls.Add(update) ChildControlsCreated = True End Sub Private Sub submit_Click(ByVal sender As Object, _ ByVal e As EventArgs) ' Update the label string. If input.Text <> String.Empty Then _contentText = input.Text & "<br />" input.Text = String.Empty DisplayContent.Text = Me.ContentText End If End Sub End Class End Namespace
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; 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; } private void submit_Click(object sender, EventArgs e) { // Update the label string. if (input.Text != String.Empty) { _contentText = input.Text + @"<br />"; input.Text = String.Empty; DisplayContent.Text = this.ContentText; } } } }
package Samples.AspNet.JSL.Controls; import System.*; import System.Security.Permissions.*; import System.Web.*; import System.Web.UI.WebControls.*; import System.Web.UI.WebControls.WebParts.*; /** @attribute AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal) */ /** @attribute AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */ public class TextDisplayWebPart extends WebPart { private String _contentText = null; private TextBox input; private Label displayContent; public TextDisplayWebPart() { this.set_AllowClose(false); } //TextDisplayWebPart /** @attribute Personalizable() @attribute WebBrowsable() */ /** @property */ public String get_ContentText() { return _contentText; } //get_ContentText /** @property */ public void set_ContentText(String value) { _contentText = value; } //set_ContentText protected void CreateChildControls() { get_Controls().Clear(); displayContent = new Label(); displayContent.set_BackColor(System.Drawing.Color.get_LightBlue()); displayContent.set_Text(this.get_ContentText()); this.get_Controls().Add(displayContent); input = new TextBox(); this.get_Controls().Add(input); Button update = new Button(); update.set_Text("Set Label Content"); update.add_Click(new EventHandler(this.Submit_Click)); this.get_Controls().Add(update); set_ChildControlsCreated(true); } //CreateChildControls private void Submit_Click(Object sender, EventArgs e) { // Update the label string. if (!(input.get_Text().Equals(""))) { _contentText = input.get_Text() + "<br />"; input.set_Text(""); displayContent.set_Text(this.get_ContentText()); } } //Submit_Click } //TextDisplayWebPart
この例の 2 番目の部分は、ASP.NET Web ページで TextDisplayWebPart コントロールを参照する方法を示しています。ブラウザにページを読み込んだ後、カスタム WebPart コントロールのタイトル バーの動詞メニューをクリックすると、close 動詞が無効になっています。これによって、ユーザーはコントロールを終了できなくなります。
<%@ page language="VB" %> <%@ register tagprefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="TextDisplayWebPartVB"%> <html> <head id="Head1" runat="server"> </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 Content WebPart" /> </zonetemplate> </asp:webpartzone> </form> </body> </html>
<%@ page language="C#" %> <%@ register tagprefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="TextDisplayWebPartCS"%> <html> <head id="Head1" runat="server"> </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 Content WebPart" /> </zonetemplate> </asp:webpartzone> </form> </body> </html>
<%@ page language="VJ#" %> <%@ register tagprefix="aspSample" Namespace="Samples.AspNet.JSL.Controls" Assembly="TextDisplayWebPartJSL"%> <html> <head id="Head1" runat="server"> </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 Content WebPart" /> </zonetemplate> </asp:webpartzone> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- WebPart コンストラクタのページへのリンク