天天看点

SharePoint 创建一个简单的Web Part 部件SharePoint 创建一个简单的Web Part 部件

SharePoint 创建一个简单的Web Part 部件

标准的Web部件有时候可以非常强大,可以执行许多函数。本文主要讲解如何使用Visual Studio 创建一个简单的Web部件。 1. 打开VS,点击文件----新建项目。 2. 选择空白SharePoint项目。命名SmallvilleWebPartProject,点击确定。选择部署为场解决方案。 3. 右击项目添加新项目。 4. 选择Web部件。 5. 命名CustomerInformation,点击添加。 6. 右击新的Web部件项目,选择添加类,命名CustomerData,点击确定。 7. 在新类中,修改代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmallvilleWebPartProject.CustomerInformation
{
    class CustomerData
    {
        public string companyName { get; set; }
        public string contactName { get; set; }
        public string contactEmail { get; set; }
        public string companyFY08Sales { get; set; }
        public string companyFY09Sales { get; set; }
    }
}
           

8. 右击核心Web部件,选择查看代码(或双击此Web部件)。 9. 修改代码如下:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace SmallvilleWebPartProject.CustomerInformation
{
    [ToolboxItemAttribute(false)]
    public class CustomerInformation : WebPart
    {
        DataGrid myCustomers = new DataGrid();
        List
    
      myCustomerDataList = new List
     
      ();

        protected override void OnPreRender(EventArgs e)
        {
            CustomerData cust1 = new CustomerData();
            CustomerData cust2 = new CustomerData();
            CustomerData cust3 = new CustomerData();
            CustomerData cust4 = new CustomerData();
            cust1.companyName = "Fabrikam";
            cust1.contactName = "Harvey Kitell";
            cust1.contactEmail = "[email protected]";
            cust1.companyFY08Sales = "$530,002.00";
            cust1.companyFY09Sales = "$650,102.00";
            myCustomerDataList.Add(cust1);
            cust2.companyName = "Contoso";
            cust2.contactName = "Ahmed Kroll";
            cust2.contactEmail = "[email protected]";
            cust2.companyFY08Sales = "$1,577,044.00";
            cust2.companyFY09Sales = "$1,653,112.00";
            myCustomerDataList.Add(cust2);
            cust3.companyName = "Acme";
            cust3.contactName = "Jansen Terrace";
            cust3.contactEmail = "[email protected]";
            cust3.companyFY08Sales = "$3,270,000.00";
            cust3.companyFY09Sales = "$2,953,100.00";
            myCustomerDataList.Add(cust3);
            cust4.companyName = "Wingtip";
            cust4.contactName = "Hally Cantrall";
            cust4.contactEmail = "[email protected]";
            cust4.companyFY08Sales = "$578,982.00";
            cust4.companyFY09Sales = "$620,100.00";
            myCustomerDataList.Add(cust4);
            myCustomers.DataSource = myCustomerDataList;
            myCustomers.DataBind();
        }

        protected override void CreateChildControls()
        {
            this.Controls.Add(myCustomers);
        }
    }
}

     
    
           

10. 然后双击CustomerInformation.webpart,修改它的标题和描述属性。

$Resources:core,ImportErrorMessage;
       
    
      
    
      
      
       
        
        
         Customer Info Web Part
        
        
        
         A Web Part that displays customer information
        
      
       
    
  
     

    
           

11. 现在你可以部署这个标准Web部件。点击生成----部署解决方案。 12. 在页面上添加。点击网站操作--编辑页面--添加Web部件。 13. 导航到Custom类别,选择 Customer Info Web Part  。点击添加。

SharePoint 创建一个简单的Web Part 部件SharePoint 创建一个简单的Web Part 部件

继续阅读