Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Connected WebParts

  Asked By: Reed    Date: Feb 05    Category: Sharepoint    Views: 3538

I'm struggling with the connection between web parts and I'm hoping
someone in the group can point me in the right direction as I have
been unsuccessful in successfully accomplishing this particular
connection between two web parts. Here's the scenario I'm trying to
establish a connection between two web parts the first web part
contains a text box and a button and let say it's used to enter a
serial number and is the provider (and will also need to be a
consumer) for the rest of the web parts which will all be consumers
besides for a GridView web part which will be a consumer and a
provider, a typical scenario will be when a user enters the serial
number in the text box and clicks the button the other web parts will
consume the serial number and populate accordingly and as for the
GridView web part it will list the different systems related to that
serial number and what I'm trying to accomplish is when the user
selects one of the other systems in the GridView I need the text box
to consume that serial number and then that serial number will be the
new serial number the other web parts will consume. The problem I'm
running into are with this circular communication between the text
box and GridView and I'm not sure how to accomplish this scenario and
I'm really hoping someone can point me in the right direction.

So as a recap here is a high level view of problem

WebPart 1 (consumer/provider that contains textbox and button that
will allow user to enter serial number that all web parts will
consume and will also act as a consumer of the selection of a related
system from the GridView in WebPart2 ) < -(Serial number) ->
WebPart2 (consumer /provider that contains a GridView and needs to
consume the serial number for WebPart1 and display related systems
based on that serial number and then if the user selects one of the
related systems I need to pass that serial number back to WebPart1
and display that in the textbox)

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Kristy Hicks     Answered On: Feb 05

When you decorate your connection  methods for provider  and consumer
there is a second parameter that names the interface. Named interfaces
only talk to interfaces of the same name. Otherwise all interfaces on a
webpart are named "default". By naming the interfaces you can have the
circular communications loop where the first web  part sends over one
interface and receives over another.

 
Answer #2    Answered By: Amanda Brown     Answered On: Feb 05

So when you talk about the named interfaces I'm asuming your talking about this

WebPart 1
[ConnectionProvider("SERIAL_NUMBER", "SERIAL_NUMBER1")]
public IWebPartField GetConnectionInterface()
{
return this;
}

[ConnectionConsumer("SERIAL_NUMBER", "SERIAL_NUMBER1")]
public void SetConnectionInterface(IWebPartField provider)
{
provider.GetFieldValue(new FieldCallback(ReceiveField));
}


WebPart 2
[ConnectionProvider("SERIAL_NUMBER", "SERIAL_NUMBER2")]
public IWebPartField GetConnectionInterface()
{
return this;
}

[ConnectionConsumer("SERIAL_NUMBER", "SERIAL_NUMBER2")]
public void SetConnectionInterface(IWebPartField provider)
{
provider.GetFieldValue(new FieldCallback(ReceiveField));
}

I have tried this but I'm still having no luck does this look right?

 
Answer #3    Answered By: Carey Everett     Answered On: Feb 05

That is what I'm talking about, but you've got the names wrong. In Web
part 1 have the provider  be Serial_number1 and consumer  be
Serial_number2 . Then reverse them in Web part  2. The named interface
provider talks to the named interface consumer with the same name.

 
Answer #4    Answered By: Anuj Lakhe     Answered On: Feb 05

Ok I'm still not successfully  getting the bi-directional communication  to work
so I wrote a simple example and here is the code does it look correct?

////////////////////////////////////////////////////////////
// Start of Web part  1
////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages.Communication;
using System.Reflection;
using System.Data;
using System.ComponentModel;
namespace WebParts
{
public class WP1 : Microsoft.SharePoint.WebPartPages.WebPart, IWebPartField
{
protected Label lblSSN1Provider;
protected Label lblSSN1Consumer;
protected textbox  txtSSN1Provider;
protected TextBox txtSSN1Consumer;
protected UpdatePanel udatePanel;
protected button  btnSSN;
private object _fieldValue;
public WP1()
{
this.ExportMode = WebPartExportMode.All;
}
public PropertyDescriptor Schema
{
get { return TypeDescriptor.GetProperties(this)["SSN_WP1"]; }
}
[ConnectionProvider("SSN", "SSN_WP1", AllowsMultipleConnections = true)]
public IWebPartField GetConnectionInterface()
{
return this;
}
[ConnectionConsumer("SSN", "SSN_WP2", AllowsMultipleConnections = true)]
public void SetConnectionInterface(IWebPartField provider)
{
provider.GetFieldValue(new FieldCallback(ReceiveField));
}
public void ReceiveField(object field)
{
if (field != null)
_fieldValue = field.ToString();
LoadData();
}
public void GetFieldValue(FieldCallback callback)
{
try
{
callback.Invoke(this.txtSSN1Provider.Text);
}
catch (Exception)
{
//throw;
}
}
public string SSN
{
get { return this.txtSSN1Provider.Text; }
set
{
if (this.txtSSN1Consumer != null)
{
this.txtSSN1Consumer.Text = value;
}
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
// Fix Form Action
this.EnsureUpdatePanelFixups();
// Create and add update panel
udatePanel = new UpdatePanel();
udatePanel.ID = "_UpdatePanel2";
udatePanel.UpdateMode = UpdatePanelUpdateMode.Always;
this.Controls.Add(udatePanel);
lblSSN1Provider = new Label();
lblSSN1Provider.ID = "lblSSN1Provider";
lblSSN1Provider.Text = "SSN1Provider";
// Create and Add textbox
txtSSN1Provider = new TextBox();
txtSSN1Provider.ID = "txtSSN1Provider";
txtSSN1Provider.EnableViewState = true;
lblSSN1Consumer = new Label();
lblSSN1Consumer.ID = "lblSSN1Consumer";
lblSSN1Consumer.Text = "SSN1Consumer";
// Create and Add textbox
txtSSN1Consumer = new TextBox();
txtSSN1Consumer.ID = "txtSSN1Consumer";
txtSSN1Consumer.EnableViewState = true;
// Create and Add button
btnSSN = new Button();
btnSSN.ID = "btnSSN";
btnSSN.Text = "Find";
// Add componets to update panel
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("<table><tr><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.lblSSN1Provider);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.txtSSN1Provider);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.lblSSN1Consumer);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.txtSSN1Consumer);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.btnSSN);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td></tr></table>"));
btnSSN.Click += new EventHandler(btnSSN_Click);
LoadData();
}
private void LoadData()
{
try
{
string serial = "";
serial = this._fieldValue.ToString();
this.txtSSN1Consumer.Text = serial;
}
catch (Exception)
{
//throw;
}
}
void btnSSN_Click(object sender, EventArgs e)
{
// left blank on pupose
}
//Fixing Form Action
private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdatePanelFixup",
"_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper=true;", true);
}
}
}
////////////////////////////////////////////////////////////
// End of Web Part 1
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
// Start of Web Part 2
////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages.Communication;
using System.Reflection;
using System.Data;
using System.ComponentModel;
namespace WebParts
{
public class WP2 : Microsoft.SharePoint.WebPartPages.WebPart, IWebPartField
{
protected Label lblSSN2Provider;
protected Label lblSSN2Consumer;
protected TextBox txtSSN2Provider;
protected TextBox txtSSN2Consumer;
protected UpdatePanel udatePanel;
protected Button btnSSN;
private object _fieldValue;
public WP2()
{
this.ExportMode = WebPartExportMode.All;
}

public PropertyDescriptor Schema
{
get { return TypeDescriptor.GetProperties(this)["SSN_WP2"]; }
}
[ConnectionProvider("SSN", "SSN_WP2", AllowsMultipleConnections = true)]
public IWebPartField GetConnectionInterface()
{
return this;
}
[ConnectionConsumer("SSN", "SSN_WP1", AllowsMultipleConnections = true)]
public void SetConnectionInterface(IWebPartField provider)
{
provider.GetFieldValue(new FieldCallback(ReceiveField));
}
public void ReceiveField(object field)
{
if (field != null)
_fieldValue = field.ToString();
LoadData();
}
public void GetFieldValue(FieldCallback callback)
{
try
{
callback.Invoke(this.txtSSN2Provider.Text);
}
catch (Exception)
{
//throw;
}
}

public string SSN
{
get { return this.txtSSN2Provider.Text; }
set
{
if (this.txtSSN2Consumer != null)
{
this.txtSSN2Consumer.Text = value;
}
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
// Fix Form Action
this.EnsureUpdatePanelFixups();
// Create and add update panel
udatePanel = new UpdatePanel();
udatePanel.ID = "_UpdatePanel2";
udatePanel.UpdateMode = UpdatePanelUpdateMode.Always;
this.Controls.Add(udatePanel);
lblSSN2Provider = new Label();
lblSSN2Provider.ID = "lblSSN2Provider";
lblSSN2Provider.Text = "SSN2Provider";
// Create and Add textbox
txtSSN2Provider = new TextBox();
txtSSN2Provider.ID = "txtSSN2Provider";
txtSSN2Provider.EnableViewState = true;
lblSSN2Consumer = new Label();
lblSSN2Consumer.ID = "lblSSN2Consumer";
lblSSN2Consumer.Text = "SSN2Consumer";
// Create and Add textbox
txtSSN2Consumer = new TextBox();
txtSSN2Consumer.ID = "txtSSN2Consumer";
txtSSN2Consumer.EnableViewState = true;
// Create and Add button
btnSSN = new Button();
btnSSN.ID = "btnSSN";
btnSSN.Text = "Find";
// Add componets to update panel
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("<table><tr><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.lblSSN2Provider);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.txtSSN2Provider);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.lblSSN2Consumer);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.txtSSN2Consumer);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td><td>"));
udatePanel.ContentTemplateContainer.Controls.Add(this.btnSSN);
udatePanel.ContentTemplateContainer.Controls.Add(new
LiteralControl("</td></tr></table>"));
btnSSN.Click += new EventHandler(btnSSN_Click);
LoadData();
}
private void LoadData()
{
try
{
string serial = "";
serial = this._fieldValue.ToString();
this.txtSSN2Consumer.Text = serial;
}
catch (Exception)
{
//throw;
}
}
void btnSSN_Click(object sender, EventArgs e)
{
// left blank on pupose
}
//Fixing Form Action
private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdatePanelFixup",
"_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper=true;", true);
}
}
}

////////////////////////////////////////////////////////////
// End of Web Part 2
////////////////////////////////////////////////////////////

 
Didn't find what you were looking for? Find more on Connected WebParts Or get search suggestion and latest updates.




Tagged: