本文将举例说明如何在wssv3中创建一个Filter consumer WebPart 来显示头条新闻。这些头条新闻内容依赖于地理区域的筛选提供者WebPart中用户的选择。
if (sessionId == null)
{
ShowError("Error opening workbook. Check the URL in " +
"OpenWorkbook, and be sure that the workbook is in " +
"a trusted location");
return;
}
// Retrieve the headlines/regions currently defined in the
// workbook from Excel Services and add them to the collection of
// headlines. Or, if there is a problem getting the headlines,
// show an error to the user.
// The code shown below assumes the following:
//
// - The opened workbook contains a worksheet named "Sheet1".
// - The "Sheet1" worksheet contains a range named "Headlines".
// - The "Headlines" range is two columns wide with the first
// column containing a news headline and the second column
// containing a region.
object[] AllHeadlines = es.GetRangeA1(sessionId, "Sheet1",
"Headlines", true, out status);
if (AllHeadlines != null)
{
unfilteredHeadlines = new List<Headline>();
foreach (object[] HeadlineRow in AllHeadlines)
{
unfilteredHeadlines.Add(
new Headline(Convert.ToString(HeadlineRow[0]),
Convert.ToString(HeadlineRow[1])));
}
}
else
{
ShowError("Error getting headlines from workbook.");
return;
}
}
private void ShowError(string message)
{
// Show an error message to the user, and remove all other
// controls from the Web Part.
lblError.Text = message;
this.Controls.Clear();
this.Controls.Add(lblError);
}
// Use the ConnectionConsumer attribute to specify a callback
// method that the Web Part framework can use to provide filter
// provider instances.
[aspnetwebparts.ConnectionConsumer("News Headlines",
"IFilterValues", AllowsMultipleConnections = true)]
public void SetConnectionInterface(
wsswebparts.IFilterValues filterProvider)
{
if (filterProvider != null)
{
// Add the filter provider to the list of providers.
this.filterProviders.Add(filterProvider);
// Tell the provider the parameter we are looking for.
List<wsswebparts.ConsumerParameter> l =
new List<wsswebparts.ConsumerParameter>();
l.Add(new wsswebparts.ConsumerParameter("Region",
wsswebparts.ConsumerParameterCapabilities.SupportsMultipleValues |
wsswebparts.ConsumerParameterCapabilities.SupportsAllValue));
filterProvider.SetConsumerParameters(
new ReadOnlyCollection<wsswebparts.ConsumerParameter>(l));
}
}
protected override void OnPreRender(EventArgs e)
{
this.EnsureChildControls();
// Call Excel Web Services to get the list of all
// news headlines.
GetHeadlinesUsingWebService();
// The filtering logic performs a union of all of the
// filters (a logical OR). If we didn't get any filter
// providers or if any of the filters send the "All" value
// (that is, provider.ParameterValues == null), we don't
// need to filter and we can return all of the
// headlines.
List<Headline> filteredHeadlines = null;
bool shouldFilter = true;
if (this.filterProviders.Count == 0)
{
shouldFilter = false;
}
else if (this.filterProviders.Count > 0)
{
foreach (wsswebparts.IFilterValues filterProvider in
this.filterProviders)
{
if (filterProvider.ParameterValues == null)
{
// Some filter sent "All"--don't bother with the
// rest of the filtering.
shouldFilter = false;
break;
}
}
}
if (!shouldFilter)
{
// The "filtered" headlines are unfiltered.
filteredHeadlines = this.unfilteredHeadlines;
}
else
{
// Just fill in the headlines that match the filters.
filteredHeadlines = new List<Headline>();
// Create a lookup from region to a list of headlines that
// correspond to that region.
Dictionary<string, List<Headline>> regionHeadlineMap =
new Dictionary<string, List<Headline>>();
foreach (Headline headline in this.unfilteredHeadlines)
{
List<Headline> headlinesForRegion = null;
if (!regionHeadlineMap.TryGetValue(headline.Region,
out headlinesForRegion))
{
headlinesForRegion = new List<Headline>();
regionHeadlineMap.Add(headline.Region,
headlinesForRegion);
}
headlinesForRegion.Add(headline);
}
foreach (wsswebparts.IFilterValues filterProvider in
this.filterProviders)
{
ReadOnlyCollection<String> values =
filterProvider.ParameterValues;
if (values != null)
{
foreach (string v in values)
{
if (v == null)
{
// This indicates the "Empty" value, which
// doesn't apply to headlines, because
// they all have regions.
}
else
{
List<Headline> matchedHeadlines;
if (regionHeadlineMap.TryGetValue(v,
out matchedHeadlines))
{
foreach (Headline matchedHeadline in
matchedHeadlines)
{
if
(!filteredHeadlines.Contains(matchedHeadline))
{
filteredHeadlines.Add(matchedHeadline);
}
}
}
}
}
}
}
}
// Display the filtered headlines.
headlinesDataGrid.DataSource = filteredHeadlines;
headlinesDataGrid.DataBind();
base.OnPreRender(e);
}
}