表示数据绑定控件SharePoint Foundation数据。
继承层次结构
System.Object
System.Web.UI.Control
System.Web.UI.DataSourceControl
Microsoft.SharePoint.WebControls.SPDataSource
命名空间: Microsoft.SharePoint.WebControls
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class SPDataSource _
Inherits DataSourceControl _
Implements ICloneable
用法
Dim instance As SPDataSource
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class SPDataSource : DataSourceControl,
ICloneable
备注
SPDataSource数据源控件数据绑定控件表示SharePoint Foundation数据。可以使用控件与数据绑定控件一起使用以从列表、 列表项和 Web 网站检索数据,以及若要显示,编辑,并修改小或无代码的数据。例如,以下标记显示 ASP.NET 页面控件配置从列表检索数据的一部分。
<SharePoint:SPDataSource ID="SPDataSource1" runat="server"
DataSourceMode="List"
UseInternalName="true"
SelectCommand="<View></View>">
<SelectParameters>
<asp:Parameter Name="WebUrl" DefaultValue="/subsite/" />
<asp:Parameter Name="ListName" DefaultValue="Contacts" />
</SelectParameters>
</SharePoint:SPDataSource>
标记的关键部分设置控件的下列属性:
-
此属性指定的数据检索模式 — 即, SharePoint Foundation数据查询的类型。在示例中, DataSourceMode属性设置为列表。其他有效模式是 ListItem、 CrossList、 ListOfLists 和网站。有关这些模式的信息,请参阅SPDataSourceMode枚举。
-
此属性接受将等操作指定为筛选、 排序顺序和的字段集功能可返回一个协作应用程序标记语言 (CAML) 片段的窗体中的查询。示例标记不指定一个查询,因此SPDataSource控件返回的所有域和所有项目。
-
此属性返回与SelectCommand一起使用的参数的集合。某些数据检索模式需要定义查询的上下文的参数。在示例中,第一个参数指定的网站位置可以找到列表,和第二个提供列表的名称的 URL。请注意,在这两情况下的 Name 属性用于指定参数和的 DefaultValue 属性用来传递值。
在示例中的参数是静态的在设计时设置。控件还可以接受参数中传递的值在运行时的解析其中的动态参数。您可以使用由 ASP.NET,包括QueryStringParameter类和ControlParameter类提供的参数类的大多数函数。例如,您可以轻松地 modfiy 示例标记以便,而不是指定的静态的参数名称列表的名称通过DropDownList与用户交互提供控制。以下标记显示如何可能属性ControlParameter对象,以便该参数值是下拉列表中的当前选择。
<SelectParameters> <asp:Parameter Name="WebUrl" DefaultValue="/subsite/" /> <asp:ControlParameter Name="ListName" ControlID="DropDownList1" PropertyName="SelectedValue" /> </SelectParameters>您还可以在参数化查询中使用动态参数。有关详细信息,请参阅SelectParameters属性。
SPDataSource类继承自DataSourceControl类。因此,可以将绑定到SPDataSource类的实例从DataBoundControl类派生的任何控件。当您使用以声明方式时,您执行此操作通过将SPDataSource控件的ID属性的值分配给该绑定的控件的DataSourceID属性下, 一个示例中所示。该示例假定网站具有两个列表,一个名为 Contacts 和另一个名为 Customers。该示例填充下拉列表中具有这些列表的名称,以便用户可以选择其中一个在运行时动态SPDataSource控件的 ListName 参数提供值。
<SharePoint:SPDataSource ID="SPDataSource1" runat="server"
DataSourceMode="List"
UseInternalName="true"
SelectCommand="<View></View>">
<SelectParameters>
<asp:Parameter Name="WebUrl" DefaultValue="/subsite/" />
<asp:ControlParameter Name="ListName" ControlID="DropDownList1" PropertyName="SelectedValue" />
</SelectParameters>
</SharePoint:SPDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Selected="True">Contacts</asp:ListItem>
<asp:ListItem>Customers</asp:ListItem>
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server" EnableViewState="false"
DataSourceID="SPDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="Title" />
<asp:BoundField HeaderText="Business Phone" DataField="WorkPhone" />
<asp:BoundField HeaderText="Email Address" DataField="Email" />
</Columns>
</asp:GridView>
以编程方式工作,您将绑定到数据源的第一个绑定的控件的DataSource属性分配SPDataSource对象,然后调用该绑定的控件的DataBind方法。
SPDataSource控件包含数据绑定逻辑,但它不直接访问数据源。由密切相关类, SPDataSourceView的实例执行该任务。SPDataSource控件的用户通常不需要与关联的SPDataSourceView对象进行交互。动态创建此对象,并通过设置SPDataSource对象的相关和并行属性间接设置其属性。
备注
与其他数据源控件,不同SPDataSource控件是与只有一个视图相关联。GetViewNames()方法始终返回一个集合包含只是一个数据视图的名称。
示例
下面的示例是一个将GridView控件绑定到一个SPDataSource控件,它设置为CrossList模式的 Web 部件。这意味着该控件的所有列表或指定类型的所有列表之间运行其查询不管它们可能会在网站集中。在这种情况下,控件查询与联系人列表模板创建的所有列表并返回它找到的每个联系人的第一个和最后一个名称。结果集填充绑定的GridView控件中的行。
Note that if you compile the example code, you can deploy the Web Part simply by copying the compiled assembly to the bin directory of the Web application. If you choose that method of deployment, be sure your project includes a reference to Microsoft.SharePoint.Security.dll. Then add the Web Part to the SafeControls list in the web.config file and elevate the Web application's trust level to WSS_Medium. For more information, see Deploying Web Parts in Windows SharePoint Services and Securing Web Parts in Windows SharePoint Services.
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint.WebControls
Public Class BoundContactViewer
Inherits WebPart
Private grid As GridView
Private datasource As SPDataSource
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
' Instantiate and add the GridView control.
Me.grid = New GridView()
Me.Controls.Add(Me.grid)
' Instantiate and add the SPDataSource control.
Me.datasource = New SPDataSource()
Me.Controls.Add(Me.datasource)
End Sub
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
' Configure the data source for a cross-list query.
Me.datasource.DataSourceMode = SPDataSourceMode.CrossList
Me.datasource.SelectCommand = "<Webs Scope='SiteCollection'></Webs>" + _
"<Lists ServerTemplate='105' ></Lists>" + _
"<View>" + _
"<ViewFields>" + _
"<FieldRef Name='Title' />" + _
"<FieldRef Name='FirstName' Nullable='TRUE'/>" + _
"</ViewFields>" + _
"<Query>" + _
"<OrderBy>" + _
"<FieldRef Name='Title' Ascending='TRUE' />" + _
"</OrderBy>" + _
"</Query>" + _
"</View>"
' Set up the field bindings
Dim boundField As BoundField = New BoundField
boundField.HeaderText = "Last Name"
boundField.DataField = "Title"
Me.grid.Columns.Add(boundField)
boundField = New BoundField()
boundField.HeaderText = "First Name"
boundField.DataField = "FirstName"
Me.grid.Columns.Add(boundField)
' Format the grid.
Me.grid.AutoGenerateColumns = False
Me.grid.AllowSorting = True
Me.grid.CssClass = "ms-listviewtable"
Me.grid.AlternatingRowStyle.CssClass = "ms-alternating"
Me.grid.Width = New Unit(50, UnitType.Percentage)
Me.grid.GridLines = GridLines.None
Me.grid.HeaderStyle.Font.Bold = True
Me.grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
' Bind to the data source.
Me.grid.DataSource = Me.datasource
Me.grid.DataBind()
Me.grid.RenderControl(writer)
End Sub
End Class
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
namespace SampleWebParts
{
public class BoundContactViewer : WebPart
{
private GridView grid;
private SPDataSource datasource;
protected override void CreateChildControls()
{
base.CreateChildControls();
// Instantiate and add the GridView control.
this.grid = new GridView();
this.Controls.Add(this.grid);
// Instantiate and add the SPDataSource control.
this.datasource = new SPDataSource();
this.Controls.Add(this.datasource);
}
protected override void RenderContents(HtmlTextWriter writer)
{
// Configure the data source for a cross-list query.
this.datasource.DataSourceMode = SPDataSourceMode.CrossList;
this.datasource.SelectCommand = "<Webs Scope=\"SiteCollection\"></Webs>" +
"<Lists ServerTemplate=\"105\" ></Lists>" +
"<View>" +
"<ViewFields>" +
"<FieldRef Name=\"Title\" />" +
"<FieldRef Name=\"FirstName\" Nullable=\"TRUE\"/>" +
"</ViewFields>" +
"<Query>" +
"<OrderBy>" +
"<FieldRef Name=\"Title\" Ascending=\"TRUE\" />" +
"</OrderBy>" +
"</Query>" +
"</View>";
// Set up the field bindings.
BoundField boundField = new BoundField();
boundField.HeaderText = "Last Name";
boundField.DataField = "Title";
this.grid.Columns.Add(boundField);
boundField = new BoundField();
boundField.HeaderText = "First Name";
boundField.DataField = "FirstName";
this.grid.Columns.Add(boundField);
// Format the grid.
this.grid.AutoGenerateColumns = false;
this.grid.AllowSorting = true;
this.grid.CssClass = "ms-listviewtable";
this.grid.AlternatingRowStyle.CssClass = "ms-alternating";
this.grid.Width = new Unit(50, UnitType.Percentage);
this.grid.GridLines = GridLines.None;
this.grid.HeaderStyle.Font.Bold = true;
this.grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
// Bind to the data source.
this.grid.DataSource = this.datasource;
this.grid.DataBind();
this.grid.RenderControl(writer);
}
}
}
线程安全性
该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。