Export (0) Print
Expand All

WebPartConnection.Provider Property

Gets a WebPart control that acts as the provider in a Web Parts connection.

Namespace:  System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

<BrowsableAttribute(False)> _
Public ReadOnly Property Provider As WebPart

Property Value

Type: System.Web.UI.WebControls.WebParts.WebPart
A WebPart that acts as the provider of data.


The length of the ProviderID property value is zero.

Every Web Parts connection consists of two WebPart controls--a consumer and a provider--and the Provider property returns a reference to the provider control in a connection.

Like its counterpart in a connection (the consumer control), the provider control is required to establish a connection. Whether you create the connection programmatically, or by declaring it in page persistence format, you must specify a control to act as the provider for a connection.

The following code example demonstrates use of the Provider property.

The code example has three parts:

  • Source code for an interface and two WebPart controls acting as the provider and the consumer for a connection.

  • A Web page to host the controls and run the code example.

  • An explanation of how to run the example page.

The first part of the code example is the source code for the interface, and the consumer and provider controls. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This code example uses dynamic compilation. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

Imports System
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Interface IZipCode

    Property ZipCode() As String 

  End Interface

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class ZipCodeWebPart
    Inherits WebPart
    Implements IZipCode
    Private zipCodeText As String = String.Empty
    Private input As TextBox
    Private send As Button

    Public Sub New()
    End Sub 

    ' Make the implemented property personalizable to save  
    ' the Zip Code between browser sessions.
    <Personalizable()> _
    Public Property ZipCode() As String _
      Implements IZipCode.ZipCode

        Return zipCodeText
      End Get 
      Set(ByVal value As String)
        zipCodeText = value
      End Set 
    End Property 

    ' This is the callback method that returns the provider.
    <ConnectionProvider("Zip Code Provider", "ZipCodeProvider")> _
    Public Function ProvideIZipCode() As IZipCode
      Return Me 
    End Function 

    Protected Overrides Sub CreateChildControls()
      input = New TextBox()
      send = New Button()
      send.Text = "Enter 5-digit Zip Code" 
      AddHandler send.Click, AddressOf Me.submit_Click

    End Sub 

    Private Sub submit_Click(ByVal sender As Object, _
      ByVal e As EventArgs)

      If input.Text <> String.Empty Then
        zipCodeText = Page.Server.HtmlEncode(input.Text)
        input.Text = String.Empty
      End If 

    End Sub 

  End Class

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class WeatherWebPart
    Inherits WebPart
    Private _provider As IZipCode
    Private _zipSearch As String 
    Private DisplayContent As Label

    ' This method is identified by the ConnectionConsumer  
    ' attribute, and is the mechanism for connecting with  
    ' the provider. 
    <ConnectionConsumer("Zip Code Consumer", "ZipCodeConsumer")> _
    Public Sub GetIZipCode(ByVal Provider As IZipCode)
      _provider = Provider
    End Sub 

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)

      If Not (Me._provider Is Nothing) Then
        _zipSearch = _provider.ZipCode.Trim()
				DisplayContent.Text = "My Zip Code is:  " + _zipSearch
      End If 

    End Sub 'OnPreRender

    Protected Overrides Sub CreateChildControls()
      DisplayContent = New Label()

    End Sub 

  End Class 

End Namespace

The second part of the code example is the Web page. Near the top is a Register directive that refers to the source code for the two dynamically compiled WebPart controls. The static connection is declared within the <StaticConnections> element on the page. Notice that in the <asp:webpartconnection> element, the ProviderID attribute has the ID of the provider control assigned to it. This is required to identify the provider for the static connection. The Button1_Click method accesses the Provider property and writes some of its details to a label.

<%@ Page Language="VB" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<script runat="server">

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)

    Dim conn As WebPartConnection = mgr.StaticConnections(0)

    lbl1.Text = "<h3>Provider Details</h3><br />" & _
      "ID: " & conn.Provider.ID & _
      "<br />" & _
      "Display Title: " & conn.Provider.Title & _
      "<br />" & _
      "Control type: " & conn.Provider.GetType().FullName

  End Sub


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
      <asp:WebPartManager ID="mgr" runat="server">
          <asp:WebPartConnection ID="conn1"
            ProviderID="zip1" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider"   />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      <asp:Button ID="Button1" runat="server" 
        Text="Provider Details" 
        OnClick="Button1_Click" />
      <br />
      <asp:Label ID="lbl1" runat="server" />

Load the page in a browser. The static connection is already created. Enter some text in the provider control, and note that the text is displayed in the consumer control. Click the Provider Details button to display some of the property values accessed through the Provider property.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
© 2015 Microsoft