程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> 如何使用VS創建新的Web Part項目

如何使用VS創建新的Web Part項目

編輯:關於ASP.NET

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,雙擊進入代碼視圖。修改屬性。

本欄目

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("<table border='0'><tr><td>");
StringBuilder sb2 = new StringBuilder();
sb2.AppendLine("</td><td>");
StringBuilder sb3 = new StringBuilder();
sb3.AppendLine("</td></tr><tr><td>");
StringBuilder sb4 = new StringBuilder();
sb4.AppendLine("</td><td></td></tr></table>");
    
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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved