创建自定义的WebPart

创建一个自定义的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:

ContractedBlock.gif ExpandedBlockStart.gif Default.aspx
None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
None.gif
None.gif
<%@ Register TagPrefix="cc1" Namespace="Samples.AspNet.CS.Controls" %>
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
<html>
None.gif
<head id="Head1" runat="server">
None.gif
</head>
None.gif
<body>
None.gif    
<form id="Form1" runat="server">
None.gif        
<asp:WebPartManager ID="WebPartManager1" runat="server" />
None.gif        
<asp:WebPartZone ID="WebPartZone1" runat="server" title="Zone 1" BorderColor="#CCCCCC" Font-Names="Verdana" Padding="6">
None.gif            
<PartTitleStyle Font-Bold="true" ForeColor="White" BackColor="#5D7B9D" Font-Size="0.8em" />
None.gif            
<PartStyle BorderWidth="1px" BorderStyle="Solid" BorderColor="#81AAF2" Font-Size="0.8em" ForeColor="#333333" />
None.gif            
<ZoneTemplate>
None.gif                
<cc1:TextDisplayWebPart runat="server" ID="textwebpart" Title="Text Content WebPart" AllowClose="False" />
None.gif            
</ZoneTemplate>
None.gif            
<PartChromeStyle BackColor="#F7F6F3" BorderColor="#E2DED6" Font-Names="Verdana" ForeColor="White" />
None.gif            
<MenuLabelHoverStyle ForeColor="#E2DED6" />
None.gif            
<EmptyZoneTextStyle Font-Size="0.8em" />
None.gif            
<MenuLabelStyle ForeColor="White" />
None.gif            
<MenuVerbHoverStyle BackColor="#F7F6F3" BorderColor="#CCCCCC" BorderStyle="Solid"
None.gif                BorderWidth
="1px" ForeColor="#333333" />
None.gif            
<HeaderStyle Font-Size="0.7em" ForeColor="#CCCCCC" HorizontalAlign="Center" />
None.gif            
<MenuVerbStyle BorderColor="#5D7B9D" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />
None.gif            
<TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />
None.gif            
<MenuPopupStyle BackColor="#5D7B9D" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana"
None.gif                Font
-Size="0.6em" />
None.gif        
</asp:WebPartZone>
None.gif    
</form>
None.gif
</body>
None.gif
</html>


TextDisplayWebPart.cs在App_Code目录中

ContractedBlock.gif ExpandedBlockStart.gif TextDisplayWebPart.cs
None.gifusing System;
None.gif
using System.Security.Permissions;
None.gif
using System.Web;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
None.gif
namespace Samples.AspNet.CS.Controls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    [AspNetHostingPermission(SecurityAction.Demand,
InBlock.gif      Level 
= AspNetHostingPermissionLevel.Minimal)]
InBlock.gif    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
InBlock.gif      Level 
= AspNetHostingPermissionLevel.Minimal)]
InBlock.gif    
public class TextDisplayWebPart : WebPart
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private String _contentText = null;
InBlock.gif        TextBox input;
InBlock.gif        Label DisplayContent;
InBlock.gif
InBlock.gif        
public TextDisplayWebPart()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.AllowClose = false;            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Personalizable(), WebBrowsable]
InBlock.gif        
public String ContentText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _contentText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _contentText = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Controls.Clear();
InBlock.gif            DisplayContent 
= new Label();
InBlock.gif            DisplayContent.BackColor 
=
InBlock.gif              System.Drawing.Color.LightBlue;
InBlock.gif            DisplayContent.Text 
= this.ContentText;
InBlock.gif            
this.Controls.Add(DisplayContent);
InBlock.gif            input 
= new TextBox();
InBlock.gif            
this.Controls.Add(input);
InBlock.gif            Button update 
= new Button();
InBlock.gif            update.Text 
= "Set Label Content";
InBlock.gif            update.Click 
+= new EventHandler(this.submit_Click);
InBlock.gif            
this.Controls.Add(update);
InBlock.gif            ChildControlsCreated 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void submit_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Update the label string.
InBlock.gif
            if (input.Text != String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _contentText 
= input.Text + @"<br />";
InBlock.gif                input.Text 
= String.Empty;
InBlock.gif                DisplayContent.Text 
= this.ContentText;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

效果图:
Text Content WebPart u
33339

转载于:https://www.cnblogs.com/hide0511/archive/2006/11/04/550157.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值