实现WebPart的编辑功能需要用到VS2005提供的EditorZone控件和EditorPart系列控件。EditorPart系列控件包括AppearanceEditorPart、BehaviorEditorPart、LayoutEditorPart、PropertyGridEditPart。
要实现WebPart的编辑功能需要几个条件:(1)用户处于共享页面范围(2)显示模式为编辑模式。
对于(1)来说,只要在Web.config文件中配置一下就OK了。
<allowusers="Tom"verbs="enterSharedScope"/> 表示用户名"Tom" 被授权共享页面范围,否则切换成编辑模式会报错。
Web.config
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><?xmlversion="1.0"?>
<configurationxmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>
<connectionStrings/>
<system.web>
<compilationdebug="true"/>
<authorization>
<denyusers="?"></deny>
</authorization>
<authenticationmode="Forms">
<formsloginUrl="Login.aspx"></forms>
</authentication>
<webParts>
<personalization>
<authorization>
<allowusers="Tom"verbs="enterSharedScope"/>
</authorization>
</personalization>
</webParts>
</system.web>
</configuration>
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><?xmlversion="1.0"?>
<configurationxmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>
<connectionStrings/>
<system.web>
<compilationdebug="true"/>
<authorization>
<denyusers="?"></deny>
</authorization>
<authenticationmode="Forms">
<formsloginUrl="Login.aspx"></forms>
</authentication>
<webParts>
<personalization>
<authorization>
<allowusers="Tom"verbs="enterSharedScope"/>
</authorization>
</personalization>
</webParts>
</system.web>
</configuration>
对于(2),在页面中添加2个LinkButton,在cs文件中切换一下显示模式,也可以用DropDownList来实现。
EditorPart系列控件中的 BehaviorEditorPart、 PropertyGridEditPart需要特别说明一下。
BehaviorEditorPart控件看名字也知道大概能做什么了,它在默认情况下可能不会显示出来,实现这个控件需要2个步骤:一、配置Web.config文件,方法上面已经说了,二、以编程的方式将页从用户级别改为共享级别,可以通过使用ToggleScope方法实现。
PropertyGridEditPart:是对自定义属性的修改。
在EditorPart中实现折叠的功能
利用Css和JavaScript脚本来实现。初始情况下,让LEGEND的显示“+”号,并隐藏区块中的内容,通过Click“+”号改变Css。
示例代码如下:
Default.aspx
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.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">
<linkhref="StyleSheet.css"rel="stylesheet"type="text/css"/>
<scripttype="text/javascript">
//为每个className值为'EditorPartTitle'的标题头创建onclick处理程序
functionCreateExpandingTitles()
{
varelements=document.getElementsByTagName("LEGEND");
for(i=0;i<elements.length;i++)
{
if(elements[i].className&&elements[i].className=="EditorPartTitle")
{
elements[i].onclick=newFunction("toggle(this);");
}
}
}
//在onload发生时,调用CreateExpandingTitles方法
if(window.addEventListener)
{
window.addEventListener('load',CreateExpandingTitles,false);
}
elseif(window.attachEvent)
{
window.attachEvent('onload',CreateExpandingTitles);
}
//单击事件处理程序
functiontoggle(titleElement)
{
varfirstChild=(titleElement.nextSibling.childNodes[0].id)
?titleElement.nextSibling.childNodes[0]
:titleElement.nextSibling.childNodes[1];
//设置图片显示及显示状态
if(firstChild.style.display=="block")
{
firstChild.style.display="none";
titleElement.style.backgroundImage="url(images/plus.gif)";
}
else
{
firstChild.style.display="block";
titleElement.style.backgroundImage="url(images/minus.gif)";
}
}
</script>
</head>
<body>
<formid="Form1"runat="server">
<asp:WebPartManagerID="WebPartManager1"runat="server"/>
<asp:LinkButtonID="LinkButton1"runat="server"Text="浏览模式(BrowseMode)|"OnClick="LinkButton1_Click"
CssClass="commonText"></asp:LinkButton>
<asp:LinkButtonID="LinkButton2"runat="server"Text="编辑模式(EditMode)"OnClick="LinkButton2_Click"
CssClass="commonText"></asp:LinkButton>
<tableborder="0"cellpadding="0"cellspacing="0"style="width:547px;height:172px">
<tr>
<tdstyle="width:268px"valign="top">
<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>
</td>
<tdstyle="width:204px">
<asp:EditorZoneID="EditorZone1"runat="server"BackColor="#EFF3FB"BorderColor="#CCCCCC"
BorderWidth="1px"Font-Names="Verdana"Padding="6">
<HeaderStyleBackColor="#D1DDF1"Font-Bold="True"Font-Size="0.8em"ForeColor="#333333"/>
<LabelStyleFont-Size="0.8em"ForeColor="#333333"/>
<HeaderVerbStyleFont-Bold="False"Font-Size="0.8em"Font-Underline="False"ForeColor="#333333"/>
<PartChromeStyleBorderColor="#D1DDF1"BorderStyle="Solid"BorderWidth="1px"/>
<ZoneTemplate>
<asp:AppearanceEditorPartID="AppearanceEditorPart1"runat="server"CssClass="EditorPartHidden"/>
<asp:BehaviorEditorPartID="BehaviorEditorPart1"runat="server"CssClass="EditorPartHidden"/>
<asp:LayoutEditorPartID="LayoutEditorPart1"runat="server"CssClass="EditorPartHidden"/>
<asp:PropertyGridEditorPartID="PropertyGridEditorPart1"runat="server"CssClass="EditorPartHidden"/>
</ZoneTemplate>
<PartStyleBorderColor="#EFF3FB"BorderWidth="5px"/>
<FooterStyleBackColor="#D1DDF1"HorizontalAlign="Right"/>
<EditUIStyleFont-Names="Verdana"Font-Size="0.8em"ForeColor="#333333"/>
<InstructionTextStyleFont-Size="0.8em"ForeColor="#333333"/>
<ErrorStyleFont-Size="0.8em"/>
<VerbStyleFont-Names="Verdana"Font-Size="0.8em"ForeColor="#333333"/>
<EmptyZoneTextStyleFont-Size="0.8em"ForeColor="#333333"/>
<PartTitleStyleFont-Bold="True"Font-Size="0.8em"ForeColor="#333333"CssClass="EditorPartTitle"/>
</asp:EditorZone>
</td>
</tr>
</table>
</form>
</body>
</html>
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.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">
<linkhref="StyleSheet.css"rel="stylesheet"type="text/css"/>
<scripttype="text/javascript">
//为每个className值为'EditorPartTitle'的标题头创建onclick处理程序
functionCreateExpandingTitles()
{
varelements=document.getElementsByTagName("LEGEND");
for(i=0;i<elements.length;i++)
{
if(elements[i].className&&elements[i].className=="EditorPartTitle")
{
elements[i].onclick=newFunction("toggle(this);");
}
}
}
//在onload发生时,调用CreateExpandingTitles方法
if(window.addEventListener)
{
window.addEventListener('load',CreateExpandingTitles,false);
}
elseif(window.attachEvent)
{
window.attachEvent('onload',CreateExpandingTitles);
}
//单击事件处理程序
functiontoggle(titleElement)
{
varfirstChild=(titleElement.nextSibling.childNodes[0].id)
?titleElement.nextSibling.childNodes[0]
:titleElement.nextSibling.childNodes[1];
//设置图片显示及显示状态
if(firstChild.style.display=="block")
{
firstChild.style.display="none";
titleElement.style.backgroundImage="url(images/plus.gif)";
}
else
{
firstChild.style.display="block";
titleElement.style.backgroundImage="url(images/minus.gif)";
}
}
</script>
</head>
<body>
<formid="Form1"runat="server">
<asp:WebPartManagerID="WebPartManager1"runat="server"/>
<asp:LinkButtonID="LinkButton1"runat="server"Text="浏览模式(BrowseMode)|"OnClick="LinkButton1_Click"
CssClass="commonText"></asp:LinkButton>
<asp:LinkButtonID="LinkButton2"runat="server"Text="编辑模式(EditMode)"OnClick="LinkButton2_Click"
CssClass="commonText"></asp:LinkButton>
<tableborder="0"cellpadding="0"cellspacing="0"style="width:547px;height:172px">
<tr>
<tdstyle="width:268px"valign="top">
<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>
</td>
<tdstyle="width:204px">
<asp:EditorZoneID="EditorZone1"runat="server"BackColor="#EFF3FB"BorderColor="#CCCCCC"
BorderWidth="1px"Font-Names="Verdana"Padding="6">
<HeaderStyleBackColor="#D1DDF1"Font-Bold="True"Font-Size="0.8em"ForeColor="#333333"/>
<LabelStyleFont-Size="0.8em"ForeColor="#333333"/>
<HeaderVerbStyleFont-Bold="False"Font-Size="0.8em"Font-Underline="False"ForeColor="#333333"/>
<PartChromeStyleBorderColor="#D1DDF1"BorderStyle="Solid"BorderWidth="1px"/>
<ZoneTemplate>
<asp:AppearanceEditorPartID="AppearanceEditorPart1"runat="server"CssClass="EditorPartHidden"/>
<asp:BehaviorEditorPartID="BehaviorEditorPart1"runat="server"CssClass="EditorPartHidden"/>
<asp:LayoutEditorPartID="LayoutEditorPart1"runat="server"CssClass="EditorPartHidden"/>
<asp:PropertyGridEditorPartID="PropertyGridEditorPart1"runat="server"CssClass="EditorPartHidden"/>
</ZoneTemplate>
<PartStyleBorderColor="#EFF3FB"BorderWidth="5px"/>
<FooterStyleBackColor="#D1DDF1"HorizontalAlign="Right"/>
<EditUIStyleFont-Names="Verdana"Font-Size="0.8em"ForeColor="#333333"/>
<InstructionTextStyleFont-Size="0.8em"ForeColor="#333333"/>
<ErrorStyleFont-Size="0.8em"/>
<VerbStyleFont-Names="Verdana"Font-Size="0.8em"ForeColor="#333333"/>
<EmptyZoneTextStyleFont-Size="0.8em"ForeColor="#333333"/>
<PartTitleStyleFont-Bold="True"Font-Size="0.8em"ForeColor="#333333"CssClass="EditorPartTitle"/>
</asp:EditorZone>
</td>
</tr>
</table>
</form>
</body>
</html>
Default.aspx.cs
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidLinkButton1_Click(objectsender,EventArgse)
{
//进入浏览模式
WebPartManager1.DisplayMode=WebPartManager.BrowseDisplayMode;
}
protectedvoidLinkButton2_Click(objectsender,EventArgse)
{
//调用ToogleScope方法,修改页面个性化范围
if(WebPartManager1.Personalization.Scope!=PersonalizationScope.Shared)
{
WebPartManager1.Personalization.ToggleScope();
}
//进入编辑模式
WebPartManager1.DisplayMode=WebPartManager.EditDisplayMode;
}
}
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidLinkButton1_Click(objectsender,EventArgse)
{
//进入浏览模式
WebPartManager1.DisplayMode=WebPartManager.BrowseDisplayMode;
}
protectedvoidLinkButton2_Click(objectsender,EventArgse)
{
//调用ToogleScope方法,修改页面个性化范围
if(WebPartManager1.Personalization.Scope!=PersonalizationScope.Shared)
{
WebPartManager1.Personalization.ToggleScope();
}
//进入编辑模式
WebPartManager1.DisplayMode=WebPartManager.EditDisplayMode;
}
}
StyleSheet.css
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->body
{}{
}
.mainTitle
{}{
font-size:12pt;
font-weight:bold;
font-family:宋体;
}
.commonText
{}{
font-size:10pt;
font-family:宋体;
text-decoration:none;
}
.littleMainTitle
{}{
font-size:10pt;
font-weight:bold;
font-family:宋体;
}
.EditorPartHidden
{}{
display:none;
}
.EditorPartTitle
{}{
background-position:left;
background-repeat:no-repeat;
background-image:url(images/plus.gif);
cursor:pointer;
padding-left:14px;
}
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->body
{}{
}
.mainTitle
{}{
font-size:12pt;
font-weight:bold;
font-family:宋体;
}
.commonText
{}{
font-size:10pt;
font-family:宋体;
text-decoration:none;
}
.littleMainTitle
{}{
font-size:10pt;
font-weight:bold;
font-family:宋体;
}
.EditorPartHidden
{}{
display:none;
}
.EditorPartTitle
{}{
background-position:left;
background-repeat:no-repeat;
background-image:url(images/plus.gif);
cursor:pointer;
padding-left:14px;
}
效果图: