WebPart Constructor
Assembly: System.Web (in system.web.dll)
The following code example demonstrates how a control that derives from the WebPart class inherits the default property values set in the base WebPart constructor, but then changes the value of one property in the constructor of the derived control.
The first part of this example contains the code for a custom WebPart control named TextDisplayWebPart. This control demonstrates how to create a simple custom WebPart control that gives you access to the features of the Web Parts control set. Note that in the constructor of the custom control, the TextDisplayWebPart.AllowClose property is set to false, which has the effect of preventing users from closing the control on a Web page. 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 Server Control.
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
The second part of the example shows how to reference the TextDisplayWebPart control in an ASP.NET Web page. After you load the page in a browser, if you click the verbs menu in the title bar of the custom WebPart control, the close verb is now disabled, which prevents users from closing the control.
<%@ 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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.