WebPartManage:可以做成一个用户控件,里面包括一个DropDownList和一个WebPartManage。用下拉框改变WebPartManage的显示模式。switch (dpType.SelectedValue),设置WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode;(还有DesignDisplayMode, CatalogDisplayMode, BrowseDisplayMode)。注:switch中的default为可选(C#编程规范中表示最好加上,做为异常处理)。default也必须加break。
WebPartZone中的“无标题”修改:比如在WebPartZone中加入一个gridview控件,然后在源码中为gridview加入title属性即可(此时.net会提示找不到gridview的title属性)。
CatalogZone:
拖曳出来的CatalogZone中需要加入PageCatalogPart(显示页面上已经删除的WebPart的列表)。ImportCatalogPart允许从Webpart文件中导入WebPart。
EditorZone:拖曳出来的EditorZone中需要加入AppearanceEditorPart。选择模式为EditDisplayMode后在WebPartZone的右上角菜单中会出现“编辑”。EditorZone可以加入对应的EditorPart。
ConnectionZone:允许用户建立WebPartZone间的通讯(面向最终用户而不是开发人员)。
参考文章:http://hi.baidu.com/meiixaohu8/blog/item/269681c85b620c1f7e3e6f2e.html
WebPart之间的通讯:
A。WebPartManager调用[ConnectionProvider]方法。
B。WebPartManager从返回参数中获得接口。
C。WebPartManager调用[ConnectionConsumer]方法,将接口传递给订阅者。
D。订阅者使用给定的接口与发布者通讯。
A。定义ISendParam.cs:
using
System;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// Summary description for ISendParam
/// </summary>
public
interface
ISendParam
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string GetParam();
}
B。新建两个用户控件,UserCtl1是接连提供者(ConnectionProvider),UserCtl2是连接用户(ConnectionConsumer)。
UserCtl1(继承ISendparam接口):
Code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserCtl1.ascx.cs" Inherits="UserCtl1" %>
<asp:TextBox ID="tbInput" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Button" OnClick="btnSubmit_Click" />
Code
public partial class UserCtl1 : System.Web.UI.UserControl, ISendParam
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
string _param = "";
protected void Page_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
[ConnectionProvider("ParamName", "ProviderName")] //定义连接区域中的活动连接
public ISendParam SendParam()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this;
}
public string GetParam() //供接受者调用
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _param;
}
protected void btnSubmit_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_param = tbInput.Text;
}
}
UserCtl2:
Code
[ConnectionConsumer("ParamName", "ReceiverName")]
public void GetParam(ISendParam provider)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
string param = provider.GetParam(); //得到提供者的参数
if (!String.IsNullOrEmpty(param)) Response.Write(param);
}
C。在测试页中加入两个WebPartZone,分别装入这两个控件。把WebPartManage1的DisplayMode设为ConnectDisplayMode,测试连接。
添加自定义的WebPart:
A。使用自定义控件:新建一个自定义控件,在原基础上再继承IWebPart接口,实现以下接口成员:
CatalogIconImageUrl:当WebPart关闭后出现在CatalogZone时的图片。
Description:鼠标悬停Title时的ToolTip。
Subtitle:WebPart小标题。
Title:WebPart正标题。
TitleIconImageUrl:WebPart题标的图标。
TitleUrl:点击Title时的跳转路径,如http://www.baidu.com或default2.aspx。
其它操作和一般用户控件一样,完成后拖入WebPartZone中。
B。使用类文件:
新建一个类,让它继承WebPart:
Code
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WebPartNameSpace
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class MyWebPart : WebPart
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TextBox tbInput;
Button btnSubmit;
Button btnDelete;
Label lbMessage;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Personalizable]
public string Text
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return lbMessage.Text;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lbMessage.Text = value;
}
}
public MyWebPart()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tbInput = new TextBox();
btnSubmit = new Button();
btnDelete = new Button();
lbMessage = new Label();
}
protected override void CreateChildControls()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Controls.Clear();
btnSubmit.Text = "确定";
btnDelete.Text = "删除";
btnSubmit.Click += new EventHandler(btnSubmit_Click); //添加事件
btnDelete.Click += new EventHandler(btnDelete_Click);
this.Controls.Add(lbMessage);
this.Controls.Add(GetBrControl()); //换行
this.Controls.Add(tbInput);
this.Controls.Add(GetBrControl());
this.Controls.Add(btnSubmit);
this.Controls.Add(btnDelete);
}
void btnSubmit_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Text += "<br />" + tbInput.Text;
}
void btnDelete_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Text = string.Empty;
}
private Control GetBrControl() //用Literal控件实际换行
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Literal liBr = new Literal();
liBr.Text = "<br />";
return liBr;
}
}
aspx页:
<%@ Register TagPrefix="cc1" Namespace="WebPartNameSpace" %>
<cc1:MyWebPart runat="server" ID="MyWebPartID" Title="MyWebPartTitle" AllowClose="False" />
WebPart的信息保存:
使用aspnet_regsql.exe创建保存webpart页面变化信息的数据库(aspnetdb)。
在aspnetdb数据库中,有两个表是用来保存个性化信息的:aspnet_PersonalizationAllUsers和aspnet_PersonalizationPerUser。
个性化体现:
A。配置web.config:增加<membership...配置(附连接字符串配置如下)。主要配置有数据提供者,连接字符串,最小密码长度。
Code
<connectionStrings>
<add name="AppConnectionString" connectionString="Server=(local);Integrated Security=True;Database=aspnetdb" />
</connectionStrings>
<membership defaultProvider="AspNetSqlProvider">
<providers>
<add connectionStringName="AppConnectionString" applicationName="/" description="" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0"
requiresUniqueEmail="false" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false"
passwordFormat="Hashed" name="AspNetSqlProvider"
type="System.Web.Security.SqlMembershipProvider"/>
</providers>
</membership>
B。点击“网站”-“ASP.NET配置”(Web.config的可视化配置工具)。进入Security进行用户配置。“选择身份验证类型”设为“通过Internet”(即Forms验证)。然后创建几个用户。
C。把<asp:Login...控件拖到网页(Default.aspx)中。运行,用不同的用户登录可以看到不同的WebPart表示。
统一页面设置:实际上,有一些网站并不是所有的用户都能自定义用户界面,而是通过站长来设置,对所有用户都起作用。
A。web.config:在webparts结点下加入<authorization...结点的配置。
<
webParts
>
<
personalization
>
<
authorization
>
<
allow users
=
"
*
"
verbs
=
"
enterSharedScope
"
/>
</
authorization
>
</
personalization
>
</
webParts
>
B。在网页(Default.aspx)中,为<asp:WebPartManager控件加入属性Personalization-InitialScope="Shared"。
C。用a用户登录,修改后再用b用户登录。b会看到a的修改。反之亦然,即实现了统一的页面显示。
为WebPart添加Verbs
即为自定义的WebPart添加右上角的下拉操作。主要是在MyWebPart类中重载Verbs属性:
Code
public override WebPartVerbCollection Verbs
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WebPartVerbCollection c = new WebPartVerbCollection();
System.Collections.ArrayList al = new System.Collections.ArrayList();
WebPartVerb v = new WebPartVerb("Hello", new WebPartEventHandler(HelloVerbHandler));
v.Text = "Hello";
al.Add(v);
return new WebPartVerbCollection(al);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void HelloVerbHandler(Object s, WebPartEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
this.Text += "<br/>" + "Hello";
}