使用Visual Studio 创建新的Web Part项目

使用Visual Studio 创建新的Web Part项目

Web Part是你将为SharePoint创建的最常见的对象之一。它是平台构建的核心基块。
1. 管理员身份打开Visual Studio,新建空白SharePoint项目。命名WroxSPProject,点击确定。部署为场解决方案,点击完成。
2. 右击选择添加新项目Web Part,命名SimpleWebPart,点击添加。
3. 在进一步前进之前,点击生成----部署解决方案。
此时,你将发现VS添加了许多项目到解决方案中。例如,它增加了feature1.feature。新的节点SimpleWebPart也被添加,它包含了许多文件。尽管你看不到,许多配置XML也更新了。

如果你双击feature节点,将打开Feature Designer。它提供了组成当前WSP包的feature图形化视图。以及设置部署层次(如网站或场)。你可以从这个视图添加或移除feature。配置部署选项以及编辑XML。因为你只添加了一个Web Part,所以只有显示一个feature。

1. 导航到SimpleWebPart.webpart,双击进入代码视图。修改属性。

    
    
    
     
     
      
      Wrox Book Delivery
     
     
    
     
     
      
      Web Part that calculates cost for delivery on Wrox developer books.
     
     

    
    
2. 打开SimpleWebPart.cs。修改代码。
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.Runtime.InteropServices;
using System.Text;

namespace WroxSPProject.SimpleWebPart
{
    [ToolboxItemAttribute(false)]
    public class SimpleWebPart : WebPart
    {
        Label lblBook = new Label();
        ListBox lstbxBooks = new ListBox();
        Label lblDelMethods = new Label();
        ListBox lstbxDeliveryMethods = new ListBox();
        Label lblDelDate = new Label();
        TextBox txtbxDelDate = new TextBox();
        Label lblFinalPrice = new Label();
        TextBox txtbxFinalPrice = new TextBox();
        Button btnCalc = new Button();

        public SimpleWebPart()
        {
        }

        protected override void CreateChildControls()
        {
            lblBook.Text = "Book Name:";
            lblFinalPrice.Text = "Final Cost:";
            lblDelDate.Text = "Del Date:";
            lblDelMethods.Text = "Del Methods:";
            btnCalc.Text = "Calc.";

            lstbxBooks.Items.Add("Professional SharePoint 2007 Development");
            lstbxBooks.Items.Add("Beginning ASP.NET Development");
            lstbxBooks.Items.Add("WPF Programming");

            lstbxDeliveryMethods.Items.Add("Ground");
            lstbxDeliveryMethods.Items.Add("Express");
            lstbxDeliveryMethods.Items.Add("Overnight");

            txtbxDelDate.Enabled = false;
            txtbxFinalPrice.Enabled = false;
            StringBuilder sb1 = new StringBuilder();
            sb1.AppendLine("
    
    
"); StringBuilder sb2 = new StringBuilder(); sb2.AppendLine(""); StringBuilder sb3 = new StringBuilder(); sb3.AppendLine("
"); StringBuilder sb4 = new StringBuilder(); sb4.AppendLine("
"); this.Controls.Add(new LiteralControl(sb1.ToString())); this.Controls.Add(lblBook); this.Controls.Add(new LiteralControl(sb2.ToString())); this.Controls.Add(lstbxBooks); this.Controls.Add(new LiteralControl(sb3.ToString())); this.Controls.Add(lblDelMethods); this.Controls.Add(new LiteralControl(sb2.ToString())); this.Controls.Add(lstbxDeliveryMethods); this.Controls.Add(new LiteralControl(sb3.ToString())); this.Controls.Add(lblDelDate); this.Controls.Add(new LiteralControl(sb2.ToString())); this.Controls.Add(txtbxDelDate); this.Controls.Add(new LiteralControl(sb3.ToString())); this.Controls.Add(lblFinalPrice); this.Controls.Add(new LiteralControl(sb2.ToString())); this.Controls.Add(txtbxFinalPrice); this.Controls.Add(new LiteralControl(sb3.ToString())); this.Controls.Add(btnCalc); this.Controls.Add(new LiteralControl(sb4.ToString())); btnCalc.Click += new EventHandler(btnCalc_Click); base.CreateChildControls(); } void btnCalc_Click(object sender, EventArgs e) { double finalCost = 0.00; double costOfDel = 0.00; double costOfBook = 0.00; double salesTax = .08; double numOfDays = 0; DateTime today = DateTime.Now; DateTime delDate; string strBook = lstbxBooks.SelectedItem.ToString(); string delMethod = lstbxDeliveryMethods.SelectedItem.ToString(); if (strBook == "Professional SharePoint 2007 Development") { costOfBook = 39.99; } else if (strBook == "Beginning ASP.NET Development") { costOfBook = 42.99; } else if (strBook == "WPF Programming") { costOfBook = 28.99; } if (delMethod == "Ground") { costOfDel = 3.99; numOfDays = 5; } else if (delMethod == "Express") { costOfDel = 7.99; numOfDays = 3; } else if (delMethod == "Overnight") { costOfDel = 11.99; numOfDays = 1; } finalCost = costOfDel + costOfBook; finalCost = Math.Round(finalCost + (finalCost * salesTax), 2)/100*100; txtbxFinalPrice.Text = "$" + finalCost.ToString(); delDate = today.AddDays(numOfDays); txtbxDelDate.Text = delDate.ToShortDateString(); } } }
3. 部署解决方案。
4. 点击视图----输出。可以看到默认生成和部署步骤过程。
5. 在站点页面添加Web Part:Wrox Book Delivery。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值