Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Connected web parts - provider empty on postback when consumer

  Asked By: Domonique    Date: May 12    Category: Sharepoint    Views: 3916

I have two web parts. WP1 is a dropdown list containing a Vendor
Number and is my provider. WP2 is a gridview that will consume the
Vendor Number from WP1 and use it to retrieve data to populate the
gridview.

This exercise started from the "Lab 3: Creating Connected Web Parts"
in the Mindsharp SharePoint Developer class using the President
example where WP2 is a consumer that populates a label control.
That all works great. But when I make the consumer web part contain
a gridview instead of a label it works when the page loads but
changing the selection in WP1 (the provider) causes WP2 to not find
any value in the interface.

From debugging, I've found the WP2 connection to the provider is
lost (interfaceFromProvider is null) after changing the selection in
WP1. I'm wondering if there is some issue with the sequence of
events which causes the gridview to not see the selected value from
the provider on a postback?

Why does this work on first load and not work after the selection is
changed in the provider web part?

Here is the code for the interface class:
//This is an interface established for exchanging vendor information
public interface IVendor
{
int Id { get; }
string Name { get; }
}

Here is the code for my provider class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Runtime.InteropServices;

namespace DummyWebParts
{
// <summary>
// This class inherits from WebPart and a proprietary interface
called IVendor
// for providing vendor data to connected web parts. It
provides the selected
// list item information to any consumer web part that
implements the IVendor
// interface
//
// Note: Uses WSS v3 WebPart class rather than ASP.NET WebPart
class in order to be
// able to save a programatically altered property
// </summary>
public class VendorProviderWebPart :
Microsoft.SharePoint.WebPartPages.WebPart, IVendor
{
private Label labelVendor = null;
private DropDownList listVendors = null;

// Implement the ID and Name properties of the IVendor
interface
#region IVendor Members

int IVendor.Id
{
get {return int.Parse(listVendors.SelectedValue);}
}

string IVendor.Name
{
get {return listVendors.SelectedItem.ToString();}
}
#endregion

protected override void CreateChildControls()
{
base.CreateChildControls();

try
{
//Create Vendor Label
this.labelVendor = new Label();
this.labelVendor.Text = "Select a Vendor:";
this.Controls.Add(labelVendor);

//Create Vendor Dropdown
this.listVendors = new DropDownList();
//Call web serice
mjrVNetCommon.VNetCommon myVNetCommon = new
mjrVNetCommon.VNetCommon();
DataSet dsVendors = myVNetCommon.GetVendors();
this.listVendors.DataSource = dsVendors.Tables[0];
this.listVendors.DataTextField = "CMBVDRNMID";
this.listVendors.DataValueField = "MBP_PF_ID";
this.listVendors.DataBind();

//Postback whenever selection changes
listVendors.AutoPostBack = true;
listVendors.SelectedIndexChanged += new EventHandler
(listVendors_SelectedIndexChanged);

this.Controls.Add(listVendors);
}
catch (Exception ex)
{
//On error, clear the controls collection and insert
the error message into the body
//of the Web Part.
this.Controls.Clear();
Literal msg = new Literal();
msg.Text = ex.Message;
this.Controls.Add(msg);
}
}

[ConnectionProvider("Vendor Selection Provider")]
public IVendor GetVendorProvider()
{
return this;
}

void listVendors_SelectedIndexChanged(object sender,
EventArgs e)
{
SelectedVendorNumber = listVendors.SelectedIndex;
this.SaveProperties = true;
}

private const int _defaultVendorNumber = 0;
private int _selectedVendorNumber = _defaultVendorNumber;

[System.Web.UI.WebControls.WebParts.WebBrowsable(false),
System.ComponentModel.DefaultValue(_defaultVendorNumber)
]
public int SelectedVendorNumber
{
get { return _selectedVendorNumber; }
set { _selectedVendorNumber = value; }
}

}
}

Here is the code for the consumer:
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;

namespace DummyWebParts {
// <summary>
// This class retrieves the Id and Name of the selected list item
from any Provider
// that implements the IVendor interface.
// </summary>
public class VendorConsumerWebPart :
System.Web.UI.WebControls.WebParts.WebPart {

IVendor interfaceFromProvider = null;
Label lbl = null;
GridView grdBuyerList = null;

public VendorConsumerWebPart()
{
this.ExportMode = WebPartExportMode.All;
}

// <summary>
// Instantiate and insert controls into the controls collection
// </summary>
protected override void CreateChildControls()
{
base.CreateChildControls();

try
{
// Create controls to show buyers
lbl = new Label();
grdBuyerList = new GridView();

// Is a web part connected to this one?
if (interfaceFromProvider != null)
{
if (interfaceFromProvider.Id > 0) {
// Get Buyers for selected vendor
lbl.Text = interfaceFromProvider.Name + "
was selected.";

// Call web service to get list of buyers
mjrVNetCommon.VNetCommon myVNetCommon = new
mjrVNetCommon.VNetCommon();
DataTable dtBuyers = new DataTable();
DataSet dsBuyers = new DataSet();
dsBuyers = myVNetCommon.GetBuyers
(interfaceFromProvider.Id);
dtBuyers = dsBuyers.Tables[0];
lbl.Text = dtBuyers.ToString();

// Bind result set to grid or display message
if (dtBuyers.Rows.Count > 0)
{
grdBuyerList.DataSource = dtBuyers;
grdBuyerList.AutoGenerateColumns = true;
grdBuyerList.Caption = "Available
Buyers";
grdBuyerList.DataBind();
}
else
{
lbl.Text = "No buyers returned for
selected vendor.";
}
}
else {
lbl.Text = "Nothing selected in provider.";
}
}
else {
lbl.Text = "No Provider Web Part connected.";
}

// Add the controls to the controls collection
this.Controls.Add(lbl);
this.Controls.Add(grdBuyerList);
}
catch (Exception ex)
{
//On error clear the controls collection
this.Controls.Clear();
Literal msg = new Literal();
msg.Text = ex.Message;
this.Controls.Add(msg);
}
}

// <summary>
// Assign the interface sent by the Provider Web Part to a
local variable cast
// to the same interface type. Note: no data is available
until the Provider Web
// Part is created which we can't depend on until later in the
page life cycle.
// The name of this public method isn't as important as the
datatype that it returns.
// </summary>
// <param name="provderData"></param>
[System.Web.UI.WebControls.WebParts.ConnectionConsumer
("Vendor")]
public void ConnectionInterface(IVendor providerData)
{
interfaceFromProvider = providerData;
}
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Chris Daniel     Answered On: May 12

Hi,
I have the same issue  in my connected  web part...please let me know if you get an y solution for it ..Thanks in advance

Siji.S
connectsiji@gmail.com

 




Tagged: