共用方式為


IFilterConsumer.SetFilter Method

NOTE: This API is now obsolete.

Responds to a request to set a filter from the SetFilter event of a provider Web Part.

Namespace:  Microsoft.SharePoint.WebPartPages.Communication
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
<ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartParameters instead")> _
Sub SetFilter ( _
    sender As Object, _
    setFilterEventArgs As SetFilterEventArgs _
)
'Usage
Dim instance As IFilterConsumer
Dim sender As Object
Dim setFilterEventArgs As SetFilterEventArgs

instance.SetFilter(sender, setFilterEventArgs)
[ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartParameters instead")]
void SetFilter(
    Object sender,
    SetFilterEventArgs setFilterEventArgs
)

Parameters

  • sender
    Type: System.Object

    A reference to the object that implements the IFilterProvider interface on the provider Web Part.

Examples

The following code example shows an implementation of the SetFilter event handler. This code example is part of a larger example provided for the IFilterConsumer interface.

' Step #9: Implement the SetFilter event handler.
' The connected provider part will call this method during its 
' PartCommunicationMain phase to set the filter on the 
' consumer Web Part.
' <param name="sender">Provider Web Part</param>
' <param name="SetFilterArgs">The args passed by the Provider</param>
Public Sub SetFilter(sender As Object, setFilterEventArgs As SetFilterEventArgs) Implements IFilterConsumer.SetFilter
   ' Ensure that all of the Web Part's controls are created.
   EnsureChildControls()
   
   ' Convert FilterExpression to the DataTable RowFilter syntax.
   If Not (setFilterEventArgs.FilterExpression Is Nothing) Then

      ' Parse the filter information.
      Dim values As String() = setFilterEventArgs.FilterExpression.Split(New [Char]() {"&"c})
      If values.Length > 1 Then
         Dim j As Integer = 1 'counts the number of Field/Value pairs
         Dim filterField As String = String.Empty
         Dim filterValue As String = String.Empty
         Dim filterAnd As String = " AND "
         _rowFilterExpression = String.Empty
         
         Dim i As Integer
         For i = 0 To values.Length - 1
            ' Clear values.
            filterField = String.Empty
            filterValue = String.Empty
      
            ' Create label portion of name/value pairs.
            Dim currField, currValue As String
            currField = FieldLabel + j.ToString() + "="
            currValue = ValueLabel + j.ToString() + "="
            j += 1
      
            ' Extract just the field name by replacing the rest of the 
            ' string with string.Empty.
            filterField = filterField + values(i).Replace(currField, String.Empty)
            
            ' Move to next item in the array which is the value 
            ' component.
            i += 1

            ' Extract just the Value by replacing the rest of the 
            ' string with string.Empty.
            filterValue = filterValue + values(i).Replace(currValue, String.Empty)
      
            ' Contruct the row filter expression.
            _rowFilterExpression += filterField + "=" + "'" + filterValue + "'" + filterAnd
         Next i 
         ' Trim Off the trailing 'And'.
         If _rowFilterExpression.Length <> 0 Then
            _rowFilterExpression = _rowFilterExpression.Substring(0, _rowFilterExpression.Length - filterAnd.Length)
         End If 
         ' Store _rowFilterExpression for use by NoFilter event.
         _cachedRowFilter.Text = _rowFilterExpression
      End If
   End If
End Sub
// Step #9: Implement the SetFilter event handler.
// The connected provider part will call this method during its 
// PartCommunicationMain phase to set the filter on the consumer 
// Web Part.
// <param name="sender">Provider Web Part</param>
// <param name="SetFilterArgs">The args passed by the Provider</param>

public void SetFilter(object sender, SetFilterEventArgs setFilterEventArgs)
{
    // Ensure that all of the Web Part's controls are created.
    EnsureChildControls();

    // Convert FilterExpression to the DataTable RowFilter syntax.
    if(setFilterEventArgs.FilterExpression != null)
    {

        // Parse the filter information.
        string[] values = setFilterEventArgs.FilterExpression.Split(new Char[] {'&'});
        if (values.Length > 1)
        {
            int j = 1;  //counts the number of Field/Value pairs
            string filterField = string.Empty;
            string filterValue = string.Empty;
            string filterAnd = " AND ";
            _rowFilterExpression = string.Empty;

            for(int i=0; i < values.Length; i++)
            {
                // Clear values.
                filterField = string.Empty;
                filterValue = string.Empty;

                // Create label portion of name/value pairs.
                string currField, currValue;
                currField = FieldLabel + j + "=";
                currValue = ValueLabel + j + "=";
                j++;  
                    
                // Extract just the field name by replacing the rest of 
                // the string with string.Empty.
                filterField = filterField + values[i].Replace(currField, string.Empty);
                    
                // Move to next item in the array which is the value 
                // component.
                i++;
                
                // Extract just the Value by replacing the rest of the 
                // string with string.Empty.
                filterValue = filterValue + values[i].Replace(currValue, string.Empty);

                // Contruct the row filter expression.
                _rowFilterExpression += filterField + "=" + "'" + filterValue + "'" + filterAnd;

            }
            // Trim Off the trailing 'And'.
            if (_rowFilterExpression.Length != 0)
                _rowFilterExpression = _rowFilterExpression.Substring(0,_rowFilterExpression.Length - filterAnd.Length);

            // Store _rowFilterExpression for use by NoFilter event.
            _cachedRowFilter.Text = _rowFilterExpression;
        }
    }
}

See Also

Reference

IFilterConsumer Interface

IFilterConsumer Members

Microsoft.SharePoint.WebPartPages.Communication Namespace