创建一个自定义的WebPart控件类似与创建自定义服务器控件,其构建的内容包括很多方面:
(1)构造函数
创建自定义的WebPart必须继承自WebPart类,在自定义类的构造函数中对WebPart的固有属性进行设置,如Title、AllowColse等。
(2)行为属性
主要包括重写AllowClose、AllowEdit、AllowConnect等“Allow”类型行为属性。虽然可以在类构造函数中对这些“Allow”类型属性设置默认值,但是通过重写属性可以更好的保护行为属性不被修改。
(3)CreatChildControls、RenderControl和RendContents方法
以上3个方法继承自Control类或者WebControl基类。通过重写这些方法,可以为自定义的WebPart添加子控件、字符串等内容,从而实现自定义WebPart的显示内容、外观和样式等。
(4)自定义操作项
WebPart类本身提供了很多个操作项,如,Close、Edit、Delete等。开发人员可以实现自定义的操作项来增加灵活性,其实现的核心是创建自定义的WebPartVerb对象。
(5)CreatEditorParts方法
如果要在编辑区域中对自定义属性进行编辑,必须实现CreatEditorParts方法。
(6)元数据属性
在自定义类中创建自定义属性的时候,可以在该属性前添加[Personalizable(), WebBrowsable]。Personalizable表示是个性化属性能够持久保存;WebBrowsable表示该属性能够在编辑模型下被用户修改。
实现一个自定义的WebPart:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
<%@RegisterTagPrefix="cc1"Namespace="Samples.AspNet.CS.Controls"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<headid="Head1"runat="server">
</head>
<body>
<formid="Form1"runat="server">
<asp:WebPartManagerID="WebPartManager1"runat="server"/>
<asp:WebPartZoneID="WebPartZone1"runat="server"title="Zone1"BorderColor="#CCCCCC"Font-Names="Verdana"Padding="6">
<PartTitleStyleFont-Bold="true"ForeColor="White"BackColor="#5D7B9D"Font-Size="0.8em"/>
<PartStyleBorderWidth="1px"BorderStyle="Solid"BorderColor="#81AAF2"Font-Size="0.8em"ForeColor="#333333"/>
<ZoneTemplate>
<cc1:TextDisplayWebPartrunat="server"ID="textwebpart"Title="TextContentWebPart"AllowClose="False"/>
</ZoneTemplate>
<PartChromeStyleBackColor="#F7F6F3"BorderColor="#E2DED6"Font-Names="Verdana"ForeColor="White"/>
<MenuLabelHoverStyleForeColor="#E2DED6"/>
<EmptyZoneTextStyleFont-Size="0.8em"/>
<MenuLabelStyleForeColor="White"/>
<MenuVerbHoverStyleBackColor="#F7F6F3"BorderColor="#CCCCCC"BorderStyle="Solid"
BorderWidth="1px"ForeColor="#333333"/>
<HeaderStyleFont-Size="0.7em"ForeColor="#CCCCCC"HorizontalAlign="Center"/>
<MenuVerbStyleBorderColor="#5D7B9D"BorderStyle="Solid"BorderWidth="1px"ForeColor="White"/>
<TitleBarVerbStyleFont-Size="0.6em"Font-Underline="False"ForeColor="White"/>
<MenuPopupStyleBackColor="#5D7B9D"BorderColor="#CCCCCC"BorderWidth="1px"Font-Names="Verdana"
Font-Size="0.6em"/>
</asp:WebPartZone>
</form>
</body>
</html>
TextDisplayWebPart.cs在App_Code目录中
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
usingSystem;
usingSystem.Security.Permissions;
usingSystem.Web;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
namespaceSamples.AspNet.CS.Controls

{
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level=AspNetHostingPermissionLevel.Minimal)]
publicclassTextDisplayWebPart:WebPart

{
privateString_contentText=null;
TextBoxinput;
LabelDisplayContent;
publicTextDisplayWebPart()

{
this.AllowClose=false;
}
[Personalizable(),WebBrowsable]
publicStringContentText

{
get
{return_contentText;}
set
{_contentText=value;}
}
protectedoverridevoidCreateChildControls()

{
Controls.Clear();
DisplayContent=newLabel();
DisplayContent.BackColor=
System.Drawing.Color.LightBlue;
DisplayContent.Text=this.ContentText;
this.Controls.Add(DisplayContent);
input=newTextBox();
this.Controls.Add(input);
Buttonupdate=newButton();
update.Text="SetLabelContent";
update.Click+=newEventHandler(this.submit_Click);
this.Controls.Add(update);
ChildControlsCreated=true;
}
privatevoidsubmit_Click(objectsender,EventArgse)

{
//Updatethelabelstring.
if(input.Text!=String.Empty)

{
_contentText=input.Text+@"<br/>";
input.Text=String.Empty;
DisplayContent.Text=this.ContentText;
}
}
}
}
效果图:
<script type="text/javascript"> <!-- var theForm = document.forms['Form1']; if (!theForm) { theForm = document.Form1; } function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } // --> </script><script src="/WebPart2/WebResource.axd?d=hVGcGlsBwbo_sCVGagOE8Q2&t=632959453400000000" type="text/javascript"></script>
|
本文介绍如何创建自定义WebPart控件,包括构造函数、行为属性、渲染方法及自定义操作项等内容。通过示例代码展示如何实现个性化属性及编辑功能。
130

被折叠的 条评论
为什么被折叠?



