使用Visual Studio 创建可视Web Part部件

使用Visual Studio 创建可视Web Part部件

可视Web Part部件是非常强大的Web 部件,它提供内置设计器创建你的用户界面。本文主要讲解如何使用Visual Studio 创建可视Web Part部件.
准备,创建一个自定义列表Stats,添加某些栏目,填充部分数据。

其中,栏目的数据类型为:

1. 打开Visual Studio,新建空白SharePoint项目SmallvilleVisualWPProject。选择部署为场解决方案。
2. 右击项目添加新项--可视Web部件PlayerStats。
3. 右击项目添加一个新类PlayerStat。点击确定。修改代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmallvilleVisualWPProject
{
    class PlayerStat
    {
        public string playerName { get; set; }
        public string gamesPlayed { get; set; }
        public string numOfGoals { get; set; }
        public string numOfAssists { get; set; }
        public string numOfPIM { get; set; }
        public string playerAVG { get; set; }
    }
}
4. 此时文件结构应该是这样的:

5. 右击PlayerStatsUserControl.ascx,选择查看代码。
6. 在工具箱拖曳相应控件,最后代码如下:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PlayerStatsUserControl.ascx.cs" Inherits="SmallvilleVisualWPProject.PlayerStats.PlayerStatsUserControl" %>

    
    




 




    
7. 设计界面应该是这样的:

8. 双击每个按钮以创建事件。
9. 双击PlayerStatsUserControl.ascx.cs来查看背后的代码。
10. 添加对应事件的代码。最后代码应该是这样的:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace SmallvilleVisualWPProject.PlayerStats
{
    public partial class PlayerStatsUserControl : UserControl
    {
        List
    
    
     
      listOfPlayerStats = new List
     
     
      
      ();
        //Set this string to your own SharePoint site URL.
        // 确保用自己服务器的URL代替这里的mySiteURL。
        string mySiteURL = "http://smallville-pc:1528/";
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnDataGridLoad_Click(object sender, EventArgs e)
        {
            statDataGrid.Width = Unit.Percentage(100);
            statDataGrid.CellPadding = 1;
            statDataGrid.HeaderStyle.Font.Bold = true;
            statDataGrid.HeaderStyle.CssClass = "ms-vh1";
            statDataGrid.GridLines = GridLines.Horizontal;
            statDataGrid.BorderWidth = Unit.Pixel(3);
            statDataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

            using (SPSite mySiteCollection = new SPSite(mySiteURL))
            {
                using (SPWeb web = mySiteCollection.OpenWeb())
                {
                    SPList myList = web.Lists["Stats"];
                    foreach (SPListItem tempListItem in myList.Items)
                    {
                        PlayerStat tempStat = new PlayerStat();
                        tempStat.playerName = tempListItem["Title"].ToString();
                        tempStat.numOfGoals = tempListItem["Goals"].ToString();
                        tempStat.numOfAssists = tempListItem["Assists"].ToString();
                        tempStat.numOfPIM = tempListItem["PIM"].ToString();
                        tempStat.gamesPlayed = tempListItem["Games"].ToString();
                        tempStat.playerAVG = calcPlayerAverage(tempStat.gamesPlayed,
                            tempStat.numOfGoals, tempStat.numOfAssists);
                        listOfPlayerStats.Add(tempStat);
                    }
                }
            }
            statDataGrid.DataSource = listOfPlayerStats;
            statDataGrid.DataBind();
        }

        protected void _btnAdd_Click(object sender, EventArgs e)
        {
            using (SPSite mySiteCollection = new SPSite(mySiteURL))
            {
                using (SPWeb web = mySiteCollection.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["Stats"];
                    SPListItem newStat = list.Items.Add();
                    newStat["Title"] = txtbxName.Text;
                    newStat["Goals"] = txtbxGoals.Text;
                    newStat["Assists"] = txtbxAssists.Text;
                    newStat["PIM"] = txtbxPIM.Text;
                    newStat["Games"] = txtbxGoals.Text;
                    newStat.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
        protected void _btnClear_Click(object sender, EventArgs e)
        {
            txtbxName.Text = "";
            txtbxGames.Text = "";
            txtbxGoals.Text = "";
            txtbxAssists.Text = "";
            txtbxPIM.Text = "";
        }
        private string calcPlayerAverage(string games, string goals, string assists)
        {
            int numGames = Int32.Parse(games);
            int numGoals = Int32.Parse(goals);
            int numAssists = Int32.Parse(assists);
            double avgStat = 0.00;
            avgStat = (numGoals * 2) + (numAssists * 1) / numGames;
            return avgStat.ToString();
        }
    }
}

     
     
    
    
11.点击生成--部署解决方案。
12. 在SharePoint站点,点击网站操作--编辑页面--添加Web部件。
13. 在Custom类选中PlayerStats,点击添加。
14. 保存后,界面应该是这样的:

点击Load后,加载了列表信息。

输入相应数据,点击Add,再点击Load。

点击Clear,所有填写的文本清空。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值