Web Part是vs2005的新控件,它的作用是可以使用户在页面上进行控件的拖放,位置的变化,对控件进行增,删,改,查.
下面我就说一下如何做一个最简单的Web Part.
首先,你需要有一个WebPartManager(工具箱里拖),他就相当于一个所有Web Part的管家,可以通过它对Web Part.的模式进行更改,也可以通过它进行Web Part.之间的通讯.
其次,有了管家后你要在网页上有给服务器控件放的地方,所以你要用到WebPartZone.你把WebPartZone防在网页的什么地方,用户就可以在什么进行拖拉控件等操作.
然后,你就可以往WebPartZone里放东西了.里面什么都可以放::服务器控件,用户自定义控件,Web自定义控件都可以.现在就该用到WebPartManager了,它有一个非常重要的属性,就是DisplayMode.
(1)BrowseDisplayMode:浏览器模式,是默认值.用户只能看不能对Web Part进行操作.
(2)EditDisplayMode:编辑模式.此模式的运行需要一个EditorZone,然后在EditorZone里可以放AppearanceEditorPart,BehaviorEditorPart,LayoutEditorPart,PropertyGridEditorPart这些控件,他们就是对Web Part进行编辑的控件,可以对Web Part的行为,外观等进行编辑.
(3)DesignDisplayMode:设计模式.在此模式下,用户就可以对控件的位置进行拖放了(在开始定义好的WebPartZone里)
(4)CatalogDisplayMode:目录模式.此模式的运行需要一个CatalogZone,CatalogZone有一个模版列,这个模版列里可以放开发人员预先定义好的控件,在HTML模式下在次模列里的控件加Title="需要显示的目录",然后用户就可以把在CatalogZone里的控件放到WebPartZone里.
(5)ConnectDisplayMode:通讯模式.此模式可以让Web Part进行通讯.可以有两种通讯,静态和动态的.需要设定好提供者和监听者.
以下是一段简单的代码:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:WebPartManager ID
=
"
WebPartManager1
"
runat
=
"
server
"
>
</
asp:WebPartManager
>
</
div
>
<
asp:WebPartZone ID
=
"
WebPartZone1
"
runat
=
"
server
"
>
<
ZoneTemplate
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
"
Label
"
></
asp:Label
>
</
ZoneTemplate
>
</
asp:WebPartZone
>
<
asp:Button ID
=
"
Button3
"
runat
=
"
server
"
OnClick
=
"
Button3_Click
"
Text
=
"
换添加模式
"
/>
<
asp:Button ID
=
"
Button4
"
runat
=
"
server
"
OnClick
=
"
Button4_Click
"
Text
=
"
换拖拉模式
"
/>
<
asp:Button ID
=
"
Button5
"
runat
=
"
server
"
OnClick
=
"
Button5_Click
"
Text
=
"
换编辑模式
"
/>
<
asp:Button ID
=
"
Button6
"
runat
=
"
server
"
OnClick
=
"
Button6_Click
"
Text
=
"
正常模式
"
/>
<
asp:Label ID
=
"
Label2
"
runat
=
"
server
"
Text
=
"
Label
"
></
asp:Label
>
<
asp:WebPartZone ID
=
"
WebPartZone2
"
runat
=
"
server
"
>
<
ZoneTemplate
>
<
asp:Calendar ID
=
"
Calendar1
"
runat
=
"
server
"
></
asp:Calendar
>
</
ZoneTemplate
>
</
asp:WebPartZone
>
<
asp:CatalogZone ID
=
"
CatalogZone1
"
runat
=
"
server
"
>
<
ZoneTemplate
>
<
asp:DeclarativeCatalogPart ID
=
"
DeclarativeCatalogPart1
"
runat
=
"
server
"
>
<
WebPartsTemplate
>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
Title
=
"
按钮
"
Text
=
"
Button
"
/>
</
WebPartsTemplate
>
</
asp:DeclarativeCatalogPart
>
</
ZoneTemplate
>
</
asp:CatalogZone
>
<
asp:EditorZone ID
=
"
EditorZone1
"
runat
=
"
server
"
>
<
ZoneTemplate
>
<
asp:AppearanceEditorPart ID
=
"
AppearanceEditorPart1
"
runat
=
"
server
"
/>
</
ZoneTemplate
>
</
asp:EditorZone
>
<
asp:ConnectionsZone ID
=
"
ConnectionsZone1
"
runat
=
"
server
"
BackColor
=
"
#FFFBD6
"
BorderColor
=
"
#CCCCCC
"
BorderWidth
=
"
1px
"
Font
-
Names
=
"
Verdana
"
Padding
=
"
6
"
>
<
LabelStyle Font
-
Size
=
"
0.8em
"
ForeColor
=
"
#333333
"
/>
<
FooterStyle BackColor
=
"
#FFCC66
"
HorizontalAlign
=
"
Right
"
/>
<
VerbStyle Font
-
Names
=
"
Verdana
"
Font
-
Size
=
"
0.8em
"
ForeColor
=
"
#333333
"
/>
<
HeaderVerbStyle Font
-
Bold
=
"
False
"
Font
-
Size
=
"
0.8em
"
Font
-
Underline
=
"
False
"
ForeColor
=
"
#333333
"
/>
<
HeaderStyle BackColor
=
"
#FFCC66
"
Font
-
Bold
=
"
True
"
Font
-
Size
=
"
0.8em
"
ForeColor
=
"
#333333
"
/>
<
InstructionTextStyle Font
-
Size
=
"
0.8em
"
ForeColor
=
"
#333333
"
/>
<
EditUIStyle Font
-
Names
=
"
Verdana
"
Font
-
Size
=
"
0.8em
"
ForeColor
=
"
#333333
"
/>
</
asp:ConnectionsZone
>
</
form
>
</
body
>
</
html
>
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
partial
class
_Default : System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
protected void Page_Load(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
protected void Button2_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.WebPartManager1.DisplayMode = WebPartManager.ConnectDisplayMode;
}
protected void Button3_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode;
}
protected void Button4_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;
}
protected void Button5_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode;
}
protected void Button6_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}