快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡亰集团平台网址大全_龟发之家论坛



经由过程前面两步打下的根基 ,

1、http://www.fengfly.com/plus/view-210571-1.html

2、http://www.fengfly.com/plus/view-210572-1.html

这里我们将应用Linq to Sharepoint来查询我们前面创建的Customer List: ACustomer.由于此List的内容是从External List:NWCustomer复制过来的,以是经由过程以它为中介来间接实现对NWCustomer的间接查询效果。下面进入操作步骤:

此处,我们要基于前面的Sharepoint 项目,新创建一个Visual Webpart (命名为WPShowCustomerInfo), 如下图:

我们要在此Webpart上添加两个控件,一个是Dropdownlist控件,它的值绑定到ACustomer的Country值上,由用户选择某个Country值,然后基于此值对ACustomer的Item值进行过滤,把过滤结果显示到另一个控件SPGridview上。效果如下图

在添加了前面的Visual Webpart后,我们先不管它的内容,我们先要在项目中引用我们的Linq to Sharepoint必要用到的DLL,它的位置在

C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPI 目录下。

名字是: Microsoft.Sharepoint.Linq.dll。如下图所示:

引用添加好之后 ,我们就必要应用SPMetal来创建我们测试网站的Entity Classes了,操作步骤如下:

首先在我们的项目上点击右键,在弹出菜单中选择打开项目在目录(Open Folder in Windows Exploer)

这样就打开了我们项目所在目录,我们必要按住Shift键不放,在Windows Exploer的任何空缺位置点击鼠标右键,在弹出的菜单中选择Open Command Windows Here,从而直接进入Windows的敕令行窗口。效果如下图:

在Windows敕令行窗口中输入SPMetal如下敕令:

spmetal.exe /web:http://sp1dev:1600/namespace:CopyListContent.WPShowCustomerInfo/code:SPLinq.cs

SPmetal的应用请拜见MSDN: SPMetal

敕令履行成功后,就在我们的项目目录下新建了一个名为SPLinq.cs的类,如下图

接下来我们必要做的便是回到我们VS2010的项目中,在此项目中添加已存在的项目,即我们上面创建的SPLinq.cs类。如下图:

然后回到我们在本文的最开初创建的Visual Web Part,做响应的改动,如下图:

WPShowCustomerInfoUserContr澳门新葡亰集团平台网址大全ol.ascx代码如下 :

%@ 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.Utilitie澳门新葡亰集团平台网址大全s" 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="WPShowCustomerInfoUserControl.ascx.cs"Inherits="CopyListContent.WPShowCustomerInfo.WPShowCustomerInfoUserControl" %>

div>table>

tr>td>

Select Country Filtertd>

td>asp:DropDownList ID="drplstCountry" runat="server" AutoPostBack="True"

OnSelectedIndexChanged="drplstCountry_SelectedIndexChanged" Height="20px"Wi澳门新葡亰集团平台网址大全dth="145px">

asp:DropDownList>td>

tr>table>

SharePoint:SPGridView ID="spGridView" runat="server" AutoGenerateColumns="false">HeaderStyle HorizontalAlign="Left" ForeColor="Navy" Font-Bold="true" />

Columns>SharePoint:SPBoundField DataField="BCSFindCompanyName"

HeaderText="CompanyName">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindContactName"HeaderText="ContactName">

SharePoint:SPBoundField>SharePoint:SPBoundField DataField="BCSFindContactTitle"

HeaderText="ContactTitle">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindCity" HeaderText="City">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindAddress" HeaderText="Address">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindRegion" HeaderText="Region">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindCountry" HeaderText="Country">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindPhone" HeaderText="Phone">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindFax" HeaderText="Fax">SharePoint:SPBoundField>

SharePoint:SPBoundField DataField="BCSFindPostalCode" HeaderText="PostalCode">SharePoint:SPBoundField>

Columns>SharePoint:SPGridView>

td> div>

WPShowCustomerInfoUserControl.ascx.cs代码如下 :

using System;

using System.Web.UI; using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint.Linq;

using Microsoft.SharePoint; using System.Linq;

namespace CopyListContent.WPShowCustomerInfo

{public partial class WPShowCustomerInfoUserControl : UserControl

{EntityList MyCustomers;

protected void Page_Load(object sender, EventArgs e){

if (!IsPostBack){

GetDistinctCustomerCountries();}

}

protected void GetDistinctCustomerCountries(){

var dc = new SPLinqDataContext(SPContext.Current.Web.Url);MyCustomers = dc.GetList("ACustomer");

var q = (from c in MyCustomers select c.BCSFindCountry).Distinct();

this.drplstCountry.DataSource = q;this.drplstCountry.DataBind();

}

protected void drplstCountry_SelectedIndexChanged(object sender, EventArgs e){

string CountryStr = this.drplstCountry.SelectedValue.ToString().Trim();GetLookUpDataByCountry(CountryStr);

}

protected void GetLookUpDataByCountry(string CountryStr){

var dc = new SPLinqDataContext(SPContext.Current.Web.Url);

var Customers = dc.GetList("ACustomer");

var customerQuery澳门新葡亰集团平台网址大全 = from customer in Customerswhere customer.BCSFindCountry == Country澳门新葡亰集团平台网址大全Str

select new{

customer.BCSFindCompanyName,customer.BCSFindContactName,

customer.BCSFindContactTitle,customer.BCSFindCity,

customer.BCSFindAddress,customer.BCSFindRegion,

customer.BCSFindCountry,customer.BCSFindPhone,

customer.BCSFindFax,customer.BCSFindPostalCode

};

spGridView.DataSource = customerQuery;spGridView.DataBind();

}}

}

从上面代码中我们可以看到,我们在两个地方用到了Linq to Sharepoint,一处是创建Dropdownlist的数据源时,应用Linq来从ACustomer中获取Country的Distinct数据值列,然后和Dropdownlist进行绑定,用于向用户供给可供过滤的前提值。一处是在用户获取到Country的过滤前提后,应用此前提来过滤ACostomer List的Items,并把过滤结果在SPGridview中显示出来,在SPGridview控件中,我们设置了针对ACustomer的绑定列。

您可能还会对下面的文章感兴趣: