Partager via


IListProvider - Interface

Remarque : cette API est désormais obsolète.

Définit les événements qu'un composant WebPart peut implémenter afin qu'il puisse fournir une liste d'entier (lignes) de données à un autre composant WebPart qui implémente l'interface IListConsumer .

Espace de noms :  Microsoft.SharePoint.WebPartPages.Communication
Assembly :  Microsoft.SharePoint (dans Microsoft.SharePoint.dll)

Syntaxe

'Déclaration
<ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartTable instead")> _
Public Interface IListProvider
'Utilisation
Dim instance As IListProvider
[ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartTable instead")]
public interface IListProvider

Remarques

L'interface de IListProvider doit être implémenté dans les WebParts qui doivent passer d'un ensemble de données qui peuvent être caractérisés comme une liste, telle qu'une table de données. Il peut être utilisé dans les scénarios où la partie consommateur a été conçue avec une compréhension des données envoyées par le fournisseur. En outre, un composant qui implémente l'interface IListProvider a la possibilité de passer des arguments de l'initialisation à la partie du consommateur. Connexion de l'interface IListProvider pour l'interface IListConsumer est une connexion directe, aucune boîte de dialogue de transformateur n'est affichée.

Exemples

L'exemple de code suivant montre un simple côté serveur IListProvider WebPart. Il peut être connecté à un ou plusieurs WebParts qui implémentent l'interface IListConsumer sur le serveur. Cet exemple affiche une liste de produits. Lorsque l'utilisateur clique sur le bouton Fire ListReady , ce composant WebPart transmet la liste complète des produits à tous les autres WebParts qui sont connectés à celui-ci.

Il existe huit étapes spécifiques à ce qui en fait un composant WebPart connectable. Ces étapes sont numérotées et commentées dans l'exemple de code suivant.

' Common .NET required namespaces
Imports System
Imports System.ComponentModel
Imports System.Web.UI

' WebPart required namespaces
Imports Microsoft.SharePoint.WebPartPages
Imports System.Xml.Serialization
Imports System.Web.UI.WebControls

' DataGrid and user interface namespaces
Imports System.Data
Imports System.Drawing

' Code Access Security namespaces
Imports System.Security
Imports Microsoft.SharePoint.Utilities

'Step #1: Reference the Communication namespace.
Imports Microsoft.SharePoint.WebPartPages.Communication

Namespace ConnectionCodeSamples
   'Step #2: Inherit from the WebPart base class and implement the IListProvider interface.
   
   Public Class ServerSideListProvider
      Inherits WebPart
      Implements IListProvider
      
      ' Step #3: Declare variables for IListProvider events.
      ' Because this class implements the IListProvider interface, it 
      ' must declare the interface members ListProviderInit, ListReady, 
      ' PartialListReady. 
      Public Event ListProviderInit As ListProviderInitEventHandler Implements IListProvider.ListProviderInit
      Public Event ListReady As ListReadyEventHandler Implements IListProvider.ListReady
      Public Event PartialListReady As PartialListReadyEventHandler Implements IListProvider.PartialListReady

      ' Declare variables for keeping track of connection state.
      Private _connected As Boolean = False
      Private _connectedWebPartTitle As String = String.Empty
      Private _registrationErrorMsg As String = "An error has occurred trying to register your connection interfaces."
      Private _registrationErrorOccurred As Boolean = False
      Private _notConnectedMsg As String = "NOT CONNECTED. To use this Web Part connect it to a List Consumer Web Part."
      
      ' Declare variables for Web Part user interface.
      Private _connectedWebPartLabel As String = "Connected to Web Part"
      Private _dataGrid As New DataGrid()
      Private _listButtonClicked As Boolean = False
      Private _listButton As Button
      
      ' Declare variables for list information.
      Private _listFieldDisplayNames() As String
      Private _listFieldNames() As String
     
      ' Step #4: Override EnsureInterfaces method and call 
      ' RegisterInterface method.
      ' EnsureInterfaces() is called by the Web Part Infrastructure 
      ' during the ASP.NET PreRender event 
      ' and allows the part to register all of its connection 
      ' interfaces.
      Public Overrides Sub EnsureInterfaces()
         ' If your Web Part is installed in the bin directory and the 
         ' Code Access Security (CAS) setting doesn't 
         ' allow Web Part Connections, an exception will be thrown. To 
         ' allow your Web Part to behave 
         ' well and continue working, a try/catch block should be used 
         ' when attempting to register interfaces.
         ' Web Part Connections will only work if the level attribute 
         ' of the <trust> tag in the 
         ' web.config file is set to WSS_Minimal, WSS_Medium, or Full. 
         ' By default a new SharePoint site
         ' is installed with the trust level set to WSS_Minimal.
         Try
            ' Register the IListProvider interface.
            ' <param name="interfaceName">Friendly name of the 
            ' interface that is being implemented.</param>
            ' <param name="interfaceType">Specifies which interface is 
            ' being implemented.</param>
            ' <param name="maxConnections">Defines how many times this 
            ' interface can be connected.</param>
            ' <param name="runAtOptions">Determines where the interface 
            ' can run.</param>
            ' <param name="interfaceObject">Reference to the object 
            ' that is implementing this interface.</param>
            ' <param name="interfaceClientReference">Name used to 
            ' reference the interface on the client. 
            ' This is a server side example so the value is set to 
            ' empty string.</param>
            ' <param name="menuLabel">Label for the interface that 
            ' appears in the UI</param>
            ' <param name="description">Description of the interface 
            ' that appears in the UI</param>
            ' <param name="allowCrossPageConnection">Specifies if the 
            ' interface can connect to a Web Part
            ' on a different page. This is an optional parameter with a 
            ' default of false. Note that only some 
            ' server-side interfaces are allowed to connect across 
            ' pages by the Web Part Infrastructure. 
            ' The IListProvider interface is not allowed to go cross 
            ' page.</param>
            RegisterInterface("MyListProviderInterface", InterfaceTypes.IListProvider, WebPart.UnlimitedConnections, ConnectionRunAt.Server, Me, "", "Provide List To", "Provides a list to a consumer Web Part.") 

         Catch se As SecurityException
            _registrationErrorOccurred = True
         End Try
      End Sub
     
      ' Step #5: Override the CanRunAt method.
      ' The CanRunAt method is called by the Web Part Infrastructure during 
      ' the ASP.NET PreRender event to determine where the Web Part can 
      ' run based on its current configuration.
      Public Overrides Function CanRunAt() As ConnectionRunAt
         ' This Web Part can run on the server.
         Return ConnectionRunAt.Server
      End Function
     
      ' Step #6: Override the PartCommunicationConnect method.
      ' The PartCommunicationConnect method is called by the Web Part 
      ' infrastructure to notify the Web Part that it
      ' is connected during the ASP.NET PreRender event. Relevant 
      ' information is passed to the part such as 
      ' the interface it is connected over, the Web Part it is being 
      ' connected to, and where the part will be running, 
      ' either client or server side. 
      ' <param name="interfaceName">Friendly name of the interface that 
      ' is being connected</param>
      ' <param name="connectedPart">Reference to the other Web Part 
      ' that is being connected to</param>
      ' <param name="connectedInterfaceName">Friendly name of the 
      ' interface on the other Web Part</param>
      ' <param name="runAt">Where the interface should execute</param>
      Public Overrides Sub PartCommunicationConnect(interfaceName As String, connectedPart As WebPart, connectedInterfaceName As String, runAt As ConnectionRunAt)
         ' Keep track of connection state.
         If interfaceName = "MyListProviderInterface" Then
            _connected = True
            _connectedWebPartTitle = SPEncode.HtmlEncode(connectedPart.Title)
         End If
      End Sub
     
      ' Step #7: Override PartCommunicationInit method.
      ' PartCommunicationInit() is called by the Web Part 
      ' infrastructure during the ASP.NET PreRender 
      ' event to allow the part to pass initialization information to 
      ' the other connected parts.
      ' It is important to always pass initialization information. Some 
      ' parts may not behave properly if this initialization 
      ' information is not received.
       Public Overrides Sub PartCommunicationInit()
            ' Ensure that all of the Web Part's controls are created.
            EnsureChildControls()

            ' Check if connected.
            If _connected Then
                ' Create the ListProviderInitEventArgs object for the 
                ' ListProviderInit event.
                Dim listProviderInitArgs As New ListProviderInitEventArgs()

                ' Set the list field names.
                listProviderInitArgs.FieldList = _listFieldNames
                listProviderInitArgs.FieldDisplayList = _listFieldDisplayNames

                ' Fire the ListProviderInit event.
                RaiseEvent ListProviderInit(Me, listProviderInitArgs)
            End If
        End Sub 

      
      ' Step #8: Override the PartCommunicationMain method.
      ' The PartCommunicationMain method is called by the Web Part 
      ' infrastructure on the client during the ASP.NET PreRender
      ' event to allow the part to pass its primary data to the other 
      ' connected parts. It is important to always fire the ListReady 
      ' or PartialListReady event. Some parts may not behave properly 
      ' if they are left waiting for this information.
       Public Overrides Sub PartCommunicationMain()
            ' Ensure that all of the Web Part's controls are created.
            EnsureChildControls()

            'Check if connected
            If _connected Then                
                    ' Create the ListReadyEventArgs object for the 
                    ' ListProviderInit event.
                    Dim listReadyArgs As New ListReadyEventArgs()

                    ' If user clicked button, send the value.
                    If _listButtonClicked Then
                        ' Set the List to the value of the table in the 
                        ' DataGrid. This is the value that will be sent 
                        ' to the consumer Web Part.
                        listReadyArgs.List = CType(_dataGrid.DataSource, DataTable)
                    Else
                        ' The user didn't click the button, so send a
                        ' null DataTable to the consumer Web Part.
                        listReadyArgs.List = Nothing
                    End If

                    ' Fire the ListReady event.
                    ' The consumer Web Part will receive the DataTable.
                    RaiseEvent ListReady(Me, listReadyArgs)                
            End If
        End Sub 

      
      Protected Overrides Sub RenderWebPart(output As HtmlTextWriter)
         ' Check for connection interface registration error.
         If _registrationErrorOccurred Then
            output.Write(_registrationErrorMsg)
            Return
         End If
         
         ' Ensure that all of the Web Part's controls are created.
         EnsureChildControls()
         
         ' Check if connected.
         If _connected Then
            ' Line break.
            output.RenderBeginTag(HtmlTextWriterTag.Br)
            output.RenderEndTag()
            
            ' Render the DataGrid control.
            _dataGrid.RenderControl(output)
            
            ' Render the Button control.
            _listButton.RenderControl(output)
            
            ' Line break.
            output.RenderBeginTag(HtmlTextWriterTag.Br)
            output.RenderEndTag()
            
            ' Render connected Web Part title.
            output.Write((_connectedWebPartLabel + ": "))
            output.RenderBeginTag(HtmlTextWriterTag.I)
            output.Write(_connectedWebPartTitle)
            output.RenderEndTag()
         
         Else
            ' The Web Part isn't connected.
            output.Write(_notConnectedMsg)
         End If
      End Sub
      
      ' Create Web Part user interface controls.
      Protected Overrides Sub CreateChildControls()
         ' Create the Button control.
         _listButton = New Button()
         _listButton.ID = "ListButton"
         _listButton.Text = "Fire ListReady"
         Controls.Add(_listButton)
         
         ' Add event handler to listen for button Click event.
         _listButtonClicked = False ' Initialize to false -- user hasn't clicked yet
         AddHandler _listButton.Click, AddressOf ListButtonClicked ' listen for Button's click event
         ' Create the DataTable.
         Dim dataTable As New DataTable()
         
         ' Add four column objects to the table.
         Dim idColumn As New DataColumn()
         idColumn.DataType = System.Type.GetType("System.Int32")
         idColumn.ColumnName = "ID"
         idColumn.Caption = "ID"
         idColumn.AutoIncrement = True
         dataTable.Columns.Add(idColumn)
         
         Dim Product As New DataColumn()
         Product.DataType = System.Type.GetType("System.String")
         Product.ColumnName = "Product"
         Product.Caption = "Product"
         dataTable.Columns.Add(Product)
         
         Dim productCategory As New DataColumn()
         productCategory.DataType = System.Type.GetType("System.String")
         productCategory.ColumnName = "Category"
         productCategory.Caption = "Category"
         dataTable.Columns.Add(productCategory)
         
         Dim Stock As New DataColumn()
         Stock.DataType = System.Type.GetType("System.Int32")
         Stock.ColumnName = "Stock"
         Stock.Caption = "Stock"
         dataTable.Columns.Add(Stock)
         
         ' Once a table has been created, use the NewRow method to 
         ' create rows.
         Dim dataRow As DataRow
         
         ' Add first row to the collection.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Aniseed Syrup"
         dataRow("Category") = "Condiments"
         dataRow("Stock") = 25
         dataTable.Rows.Add(dataRow)
         
         ' Add a second row
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Vegie-spread"
         dataRow("Category") = "Condiments"
         dataRow("Stock") = 10
         dataTable.Rows.Add(dataRow)
         
         ' Add a third row.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Outback Lager"
         dataRow("Category") = "Beverages"
         dataRow("Stock") = 30
         dataTable.Rows.Add(dataRow)
         
         ' Add a fourth row.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Boston Crab Meat"
         dataRow("Category") = "Seafood"
         dataRow("Stock") = 10
         dataTable.Rows.Add(dataRow)
         
         ' And a fifth row.
         dataRow = dataTable.NewRow()
         dataRow("Product") = "Tofu"
         dataRow("Category") = "Produce"
         dataRow("Stock") = 41
         dataTable.Rows.Add(dataRow)
         
         ' Set the DataGrid's DataSource.
         _dataGrid.DataSource = dataTable
         _dataGrid.ID = "DataGrid"
         
         ' Format the DataGrid.
         _dataGrid.HeaderStyle.Font.Bold = True
         _dataGrid.HeaderStyle.ForeColor = Color.DarkBlue
         _dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
         _dataGrid.AlternatingItemStyle.BackColor = Color.Beige
         
         _dataGrid.DataBind()
         Controls.Add(_dataGrid)
         
         ' Set the DataTable FieldName information.
         'This information will be passed to the Consumer by firing the 
         ' ListProviderInit event.
         Dim columnCount As Integer = dataTable.Columns.Count
         _listFieldNames = New String(columnCount) {}
         _listFieldDisplayNames = New String(columnCount) {}
         
         Dim i As Integer
         For i = 0 To columnCount - 1
            _listFieldNames(i) = dataTable.Columns(i).ColumnName
            _listFieldDisplayNames(i) = dataTable.Columns(i).Caption
         Next i
      End Sub
        
      ' The Button OnClick event handler
      ' <param name="sender">The Button object</param>
      ' <param name="e">The Event Arguments</param>
      Private Sub ListButtonClicked(sender As Object, e As EventArgs)
         _listButtonClicked = True 'user clicked button, set to true
      End Sub
   End Class
End Namespace
// Common .NET required namespaces
using System;
using System.ComponentModel;
using System.Web.UI;

// WebPart required namespaces
using Microsoft.SharePoint.WebPartPages;
using System.Xml.Serialization;
using System.Web.UI.WebControls;

// DataGrid and user interface namespaces
using System.Data;
using System.Drawing;

// Code Access Security namespaces
using System.Security;
using Microsoft.SharePoint.Utilities;

//Step #1: Reference the Communication namespace.
using Microsoft.SharePoint.WebPartPages.Communication;


namespace ConnectionCodeSamples
{
    //Step #2: Inherit from the WebPart base class and implement the 
    //IListProvider interface.
    public class ServerSideListProvider : WebPart, IListProvider
    {    
        
        // Step #3: Declare variables for IListProvider events.
        // Because this class implements the IListProvider interface, 
        // it must declare the interface members ListProviderInit, 
        // ListReady, PartialListReady. 
        
        public event ListProviderInitEventHandler ListProviderInit;
        public event ListReadyEventHandler ListReady;
        public event PartialListReadyEventHandler PartialListReady; 

        // Declare variables for keeping track of connection state.
        private bool _connected = false;
        private string _connectedWebPartTitle = string.Empty;
        private string _registrationErrorMsg = "An error has occurred trying to register your connection interfaces.";
        private bool _registrationErrorOccurred = false;
        private string _notConnectedMsg = "NOT CONNECTED. To use this Web Part connect it to a List Consumer Web Part.";

        // Declare variables for Web Part user interface.
        private string _connectedWebPartLabel = "Connected to Web Part";    
        private DataGrid _dataGrid = new DataGrid();
        private bool _listButtonClicked = false;
        private Button _listButton;
        
        // Declare variables for list information.
        private string[] _listFieldDisplayNames;
        private string[] _listFieldNames;
        
        
        // Step #4: Override EnsureInterfaces method and call RegisterInterface method.
        // EnsureInterfaces() is called by the Web Part Infrastructure during the ASP.NET PreRender event 
        // and allows the part to register all of its connection interfaces.
        public override void EnsureInterfaces()
        {
            // If your Web Part is installed in the bin directory and 
            // the Code Access Security (CAS) setting doesn't 
            // allow Web Part Connections, an exception will be thrown. 
            // To allow your Web Part to behave 
            // well and continue working, a try/catch block should be 
            // used when attempting to register interfaces.
            // Web Part Connections will only work if the level 
            // attribute of the <trust> tag in the 
            // web.config file is set to WSS_Minimal, WSS_Medium, or 
            // Full. By default a new SharePoint site
            // is installed with the trust level set to WSS_Minimal.
            try
            {
                // Register the IListProvider interface.
                // <param name="interfaceName">Friendly name of the 
                // interface that is being implemented.</param>
                // <param name="interfaceType">Specifies which 
                // interface is being implemented.</param>
                // <param name="maxConnections">Defines how many times 
                // this interface can be connected.</param>
                // <param name="runAtOptions">Determines where the 
                // interface can run.</param>
                // <param name="interfaceObject">Reference to the 
                // object that is implementing this interface.</param>
                // <param name="interfaceClientReference">Name used to 
                // reference the interface on the client. 
                // This is a server side example so the value is set to 
                // empty string.</param>
                // <param name="menuLabel">Label for the interface 
                // which appears in the UI</param>
                // <param name="description">Description of the 
                // interface which appears in the UI</param>
                // <param name="allowCrossPageConnection">Specifies if 
                // the interface can connect to a Web Part
                // on a different page. This is an optional parameter 
                // with a default of false. Note that only some 
                // server side interfaces are allowed to connect across 
                // pages by the Web Part Infrastructure. 
                // The IListProvider interface is not allowed to go 
                // cross page.</param>
                RegisterInterface("MyListProviderInterface",                //InterfaceName    
                    InterfaceTypes.IListProvider,                           //InterfaceType
                    WebPart.UnlimitedConnections,                           //MaxConnections
                    ConnectionRunAt.Server,                                 //RunAtOptions
                    this,                                                   //InterfaceObject
                    "",                                                     //InterfaceClientReference
                    "Provide List To",                                      //MenuLabel
                    "Provides a list to a consumer Web Part.");             //Description
            }
            catch(SecurityException se)
            {
                _registrationErrorOccurred = true;
            }
        }

        
        // Step #5: Override the CanRunAt method.
        // The CanRunAt method is called by the Web Part Infrastructure 
        // during the ASP.NET PreRender event
        // to determine where the Web Part can run based on its current 
        // configuration.
         public override ConnectionRunAt CanRunAt()
        {
            // This Web Part can run on the server.
            return ConnectionRunAt.Server;
        }

        // Step #6: Override the PartCommunicationConnect method.
        // The PartCommunicationConnect method is called by the Web 
        // Part infrastructure to notify the Web Part that it
        // is connected during the ASP.NET PreRender event. Relevant 
        // information is passed to the part such as 
        // the interface it is connected over, the Web Part it is being 
        // connected to, and where the part will be running, 
        // either client or server side. 
        // <param name="interfaceName">Friendly name of the interface 
        // that is being connected</param>
        // <param name="connectedPart">Reference to the other Web Part 
        // that is being connected to</param>
        // <param name="connectedInterfaceName">Friendly name of the 
        // interface on the other Web Part</param>
        // <param name="runAt">Where the interface should 
        // execute</param>
        public override void PartCommunicationConnect(
            string interfaceName,
            WebPart connectedPart,
            string connectedInterfaceName,
            ConnectionRunAt runAt)
        {
            // Keep track of connection state.
            if (interfaceName == "MyListProviderInterface")
            {
                _connected = true;
                _connectedWebPartTitle = SPEncode.HtmlEncode(connectedPart.Title);
            }
        }

        // Step #7: Override PartCommunicationInit method.
        // PartCommunicationInit() is called by the Web Part 
        // infrastructure during the ASP.NET PreRender 
        // event to allow the part to pass initialization information 
        // to the other connected parts.
        // It is important to always pass initialization information. 
        // Some parts may not behave properly if this initialization 
        // information is not received.
        
        public override void PartCommunicationInit()
        {
            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            // Check if connected.
            if(_connected)
            {
                // If there is a listener, fire ListProviderInit event.
                if (ListProviderInit != null)
                {
                    //Create the ListProviderInitEventArgs object for 
                    //the ListProviderInit event.
                    ListProviderInitEventArgs listProviderInitArgs = new ListProviderInitEventArgs();
                    
                    // Set the list field names.
                    listProviderInitArgs.FieldList = _listFieldNames;
                    listProviderInitArgs.FieldDisplayList = _listFieldDisplayNames;

                    // Fire the ListProviderInit event.
                    ListProviderInit(this, listProviderInitArgs);
                }
            }
        }

        // Step #8: Override the PartCommunicationMain method.
        // The PartCommunicationMain method is called by the Web Part 
        // infrastructure on the client during the ASP.NET PreRender
        // event to allow the part to pass its primary data to the 
        // other connected parts.
        // It is important to always fire the ListReady or 
        // PartialListReady event. Some parts
        // may not behave properly if they are left waiting for this 
        // information.
        public override void PartCommunicationMain()
        {
            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            //Check if connected
            if(_connected)
            {
                // If there is a listener, fire the ListReady event.
                if (ListReady != null)
                {
                    // Create the ListReadyEventArgs object for the 
                    // ListProviderInit event.
                    ListReadyEventArgs listReadyArgs = new ListReadyEventArgs();

                    // If user clicked button, send the value.
                    if (_listButtonClicked)
                    {
                        // Set the List to the value of the table in 
                        // the DataGrid.
                        // This is the value that will be sent to the 
                        // consumer Web Part.
                        listReadyArgs.List = ((DataTable)_dataGrid.DataSource);
                    }
                    else
                    {
                        // The user didn't click the button, so send a
                        // null DataTable to the consumer Web Part.
                        listReadyArgs.List = null;
                    }

                    // Fire the ListReady event.
                    // The consumer Web Part will receive the 
                    // DataTable.
                    ListReady(this, listReadyArgs);
                }
            }
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
            // Check for connection interface registration error.
            if (_registrationErrorOccurred)
            {
                output.Write(_registrationErrorMsg);
                return;
            }

            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            // Check if connected.
            if(_connected)
            {
                // Line break.
                output.RenderBeginTag(HtmlTextWriterTag.Br);
                output.RenderEndTag();

                // Render the DataGrid control.
                _dataGrid.RenderControl(output);

                // Render the Button control.
                _listButton.RenderControl(output);

                // Line break.
                output.RenderBeginTag(HtmlTextWriterTag.Br);
                output.RenderEndTag();

                // Render connected Web Part title.
                output.Write(_connectedWebPartLabel + ": ");
                output.RenderBeginTag(HtmlTextWriterTag.I);
                output.Write(_connectedWebPartTitle);
                output.RenderEndTag();

            }
            else
            {
                // The Web Part isn't connected.
                output.Write(_notConnectedMsg);
            }
        }

        // Create Web Part user interface controls.
        protected override void CreateChildControls()
        {
            // Create the Button control.
            _listButton = new Button();
            _listButton.ID = "ListButton";
            _listButton.Text = "Fire ListReady";
            Controls.Add(_listButton);

            // Add event handler to listen for button Click event.
            _listButtonClicked = false; // Initialize to false -- user hasn't clicked yet
            _listButton.Click += new EventHandler(ListButtonClicked); // listen for Button's click event
            
            // Create the DataTable.
            DataTable dataTable = new DataTable();

            // Add four column objects to the table.
            DataColumn idColumn = new  DataColumn();
            idColumn.DataType = System.Type.GetType("System.Int32");
            idColumn.ColumnName = "ID";
            idColumn.Caption = "ID";
            idColumn.AutoIncrement = true;
            dataTable.Columns.Add(idColumn);

            DataColumn Product = new DataColumn();
            Product.DataType = System.Type.GetType("System.String");
            Product.ColumnName = "Product";
            Product.Caption = "Product";
            dataTable.Columns.Add(Product);

            DataColumn productCategory = new DataColumn();
            productCategory.DataType = System.Type.GetType("System.String");
            productCategory.ColumnName = "Category";
            productCategory.Caption = "Category";
            dataTable.Columns.Add(productCategory);

            DataColumn Stock = new DataColumn();
            Stock.DataType = System.Type.GetType("System.Int32");
            Stock.ColumnName = "Stock";
            Stock.Caption = "Stock";
            dataTable.Columns.Add(Stock);

            // Once a table has been created, use the NewRow method to 
            // create rows.
            DataRow dataRow;
            
            // Add first row to the collection.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Aniseed Syrup";
            dataRow["Category"] = "Condiments";
            dataRow["Stock"] = 25;
            dataTable.Rows.Add(dataRow);

            // Add a second row
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Vegie-spread";
            dataRow["Category"] = "Condiments";
            dataRow["Stock"] = 10;
            dataTable.Rows.Add(dataRow);

            // Add a third row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Outback Lager";
            dataRow["Category"] = "Beverages";
            dataRow["Stock"] = 30;
            dataTable.Rows.Add(dataRow);

            // Add a fourth row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Boston Crab Meat";
            dataRow["Category"] = "Seafood";
            dataRow["Stock"] = 10;
            dataTable.Rows.Add(dataRow);

            // And a fifth row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Tofu";
            dataRow["Category"] = "Produce";
            dataRow["Stock"] = 41;
            dataTable.Rows.Add(dataRow);

            // Set the DataGrid's DataSource.
            _dataGrid.DataSource = dataTable;
            _dataGrid.ID = "DataGrid";

            // Format the DataGrid.
            _dataGrid.HeaderStyle.Font.Bold = true;
            _dataGrid.HeaderStyle.ForeColor = Color.DarkBlue;
            _dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            _dataGrid.AlternatingItemStyle.BackColor = Color.Beige;

            _dataGrid.DataBind();
            Controls.Add(_dataGrid);

            // Set the DataTable FieldName information.
            //This information will be passed to the Consumer by firing 
            // the ListProviderInit event.
            int columnCount = dataTable.Columns.Count;
            _listFieldNames = new string[columnCount];
            _listFieldDisplayNames = new string[columnCount];

            for(int i = 0; i < columnCount; i++)
            {
                _listFieldNames[i] = dataTable.Columns[i].ColumnName;
                _listFieldDisplayNames[i] = dataTable.Columns[i].Caption;
            }
        }

        
        // The Button OnClick event handler
        
        // <param name="sender">The Button object</param>
        // <param name="e">The Event Arguments</param>
        private void ListButtonClicked(object sender, EventArgs e)
        {
            _listButtonClicked = true; //user clicked button, set to true
        }
    }
}

Voir aussi

Référence

IListProvider - Membres

Microsoft.SharePoint.WebPartPages.Communication - Espace de noms