编辑代码 编辑webpart代码主要包括以下个步骤: 1. 编辑webpart的C#代码(指.cs文件) 2. 编辑webpart描述文件(Template/Features/{解决方案名称}/WebParts下的.webpar文件) 3. 编辑feature.xml(Template/Features/{解决方案名称}/下的feature.xml文件) 4. 编辑WebParts.xml(Template/Features/{解决方案名称}/WebParts下的WebParts.xml文件) 好,现在让我们开始一个例子。这个例子是创建一个使用了LINQ和自动属性等DotNet3.5特性技术,用来显示一个数组中小于5的数字。效果如下:
既然要用LINQ,第一步当然是要添加LINQ的引用拉,注意!LINQ的引用是System.Core
![](https://i-blog.csdnimg.cn/blog_migrate/e1a2130f8a8199c039d2f8e4462383f0.jpeg) 好,万事具备,我们开始按照刚才讲的4个步骤做吧, 第1步,编辑webpart的C#代码(指.cs文件)。 先在解决方案根目录下创建一个名为HelloDotNet35.CS文件。然后把这个文件的代码修改成下面内容:
1
using
System;
2
using
System.Web;
3
using
System.Web.UI;
4
using
System.Linq;
5
using
Microsoft.SharePoint;
6
using
Microsoft.SharePoint.WebControls;
7
using
Microsoft.SharePoint.WebPartPages;
8
using
System.Web.UI.WebControls;
9
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
10
namespace
Adalovelacer.WebpartTest
11
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{ 12 public class HelloDoNet35 : WebPart 13![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif) { 14![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif) /**//// 15 /// 使用Dotnet3.5的自动属性功能 16 /// 17![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif) public string Introduction { get; set; } 18![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif) 19 protected override void Render(HtmlTextWriter writer) 20![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif) { 21 Introduction = "使用Dotnet3.5的自动属性,LINQ功能 "; 22 writer.Write(Introduction); 23 24![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif) int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 25![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif) 26 writer.Write("在数组, "); 27 foreach (int item in numbers) 28![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif) { 29 writer.Write(item + " "); 30 } 31 writer.Write(" 中,小于5的数字有 "); 32![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif) 33![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif) 34![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif) 使用LINQ检索列表#region 使用LINQ检索列表 35![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif) 36 var lowNums = from num in numbers 37 where num < 5 38 select num; 39![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif) 40 #endregion 41![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif) 42 foreach (int i in lowNums) 43![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif) { 44 writer.Write(i + " "); 45 } 46 } 47 } 48 }
49
第2步,编辑webpart描述文件(Template/Features/{解决方案名称}/WebParts下的.webpar文件) 把Template/Features/Adalovelacer.WebpartTest/WebParts/CustomWebPart1.webpart复制后,再粘贴在相同目录下,并重命名为:HelloDotNet35.webpart。 然后打开HelloDotNet35.webpart,修改成如下内容:
<?
xml version="1.0" encoding="utf-8"
?>
<!--
Created by STSDEV at 2008-3-27 22:31:24
-->
<
webParts
>
<
webPart
xmlns
="http://schemas.microsoft.com/WebPart/v3"
>
<
metaData
>
<
type
name
="Adalovelacer.WebpartTest.HelloDoNet35, Adalovelacer.WebpartTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=03bd28a31f440b7b"
/>
<
importErrorMessage
>
Error importing Web Part
</
importErrorMessage
>
</
metaData
>
<
data
>
<
properties
>
<
property
name
="Title"
type
="string"
>
HelloDoNet35
</
property
>
<
property
name
="Description"
type
="string"
>
欢迎大家一起探讨Webcontorl/WebpPart开发,欢迎加入msn群:group182777@msnzone.cn
</
property
>
<
property
name
="ChromeState"
type
="chromestate"
>
Normal
</
property
>
<
property
name
="AllowZoneChange"
type
="bool"
>
True
</
property
>
<
property
name
="AllowHide"
type
="bool"
>
True
</
property
>
<
property
name
="ExportMode"
type
="exportmode"
>
All
</
property
>
</
properties
>
</
data
>
</
webPart
>
</
webParts
>
第3步,编辑feature.xml(Template/Features/{解决方案名称}/下的feature.xml文件) 我们打开Template/Features/Adalovelacer.WebpartTest/feature.xml。修改成:
<?
xml version="1.0" encoding="utf-8"
?>
<!--
Created by STSDEV at 2008-3-27 22:31:24
-->
<
Feature
Id
="A0D121E7-B848-4A96-85C7-A9B6A530B2F5"
Title
="A sample feature: Adalovelacer.WebpartTest"
Description
="This SharePoint solution was created by the STSDEV utility (http://codeplex.com/stsdev)"
Version
="1.0.0.0"
Scope
="Site"
Hidden
="false"
ImageUrl
="Adalovelacer.WebpartTest/AfricanPith32.gif"
xmlns
="http://schemas.microsoft.com/sharepoint/"
>
<
ElementManifests
>
<
ElementManifest
Location
="WebParts.xml"
/>
<
ElementFile
Location
="WebParts/CustomWebPart1.webpart"
/>
<
ElementFile
Location
="WebParts/CustomWebPart2.webpart"
/>
<!--
此处加上刚才完成的webpart文件
-->
<
ElementFile
Location
="WebParts/HelloDoNet35.webpart"
/>
<!---->
</
ElementManifests
>
</
Feature
>
第4步,编辑WebParts.xml(Template/Features/{解决方案名称}/WebParts下的WebParts.xml
我们打开Template/Features/Adalovelacer.WebpartTest/WebParts.xml。修改成:
1
<?
xml version="1.0" encoding="utf-8"
?>
2
<!--
Created by STSDEV at 2008-3-27 22:31:24
-->
3
<
Elements
xmlns
="http://schemas.microsoft.com/sharepoint/"
>
4
<
Module
Name
="Adalovelacer.WebpartTest"
List
="113"
Url
="_catalogs/wp"
Path
="WebParts"
RootWebOnly
="True"
>
5
<
File
Url
="CustomWebPart1.webpart"
Type
="GhostableInLibrary"
>
6
<
Property
Name
="Group"
Value
="Adalovelacer.WebpartTest"
/>
7
</
File
>
8
<
File
Url
="CustomWebPart2.webpart"
Type
="GhostableInLibrary"
>
9
<
Property
Name
="Group"
Value
="Adalovelacer.WebpartTest"
/>
10
</
File
>
11
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
12
<!--
此处加上刚才完成的webpart文件
-->
13
<
File
Url
="HelloDoNet35.webpart"
Type
="GhostableInLibrary"
>
14
<
Property
Name
="Group"
Value
="Adalovelacer.WebpartTest"
/>
15
</
File
>
16
<!---->
17
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
18
</
Module
>
19
</
Elements
>
好,编译一下,全部通过了,至此代码编写工作全部完成。但是请不要关闭你的vs2008,我们还要用它来部署。
向着理想勇敢前进!
|