Creating connected web part in SharePoint 2010

Steps:
1. Create interface.
2. Create provider web part by implementing the interface created.
3. Create consumer web part with constructor having parameter as interface created.

Code blocks below:-

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConnectedWebPartSolution
{
public interface ITextBoxString
{
string TextBoxString { get; set; }

}

}
——— ————————————————————-
2. Provider web part StringProvider.cs

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace ConnectedWebPartSolution.Webparts.StringProvider
{

public class StringProvider : System.Web.UI.WebControls.WebParts.WebPart, ITextBoxString
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @”~/_CONTROLTEMPLATES/ConnectedWebPartSolution.Webparts/StringProvider/StringProviderUserControl.ascx”;
private TextBox myTextBox;
private String _textBoxString = String.Empty;
private Button myButton;

[Personalizable()]
public string TextBoxString
{
get
{
return _textBoxString;
}
set
{
_textBoxString = value;
}
}

protected override void CreateChildControls()
{
Controls.Clear();
myTextBox = new TextBox();
Controls.Add(myTextBox);

myButton = new Button();
myButton.Text = “Change Text”;
Controls.Add(myButton);
myButton.Click += new EventHandler(myButton_Click);
}
void myButton_Click(object sender, EventArgs e)
{
if (myTextBox.Text != String.Empty)
{
TextBoxString = myTextBox.Text;
myTextBox.Text = String.Empty;
}
}

[ConnectionProvider(“Provider for String From TextBox”)]
public ITextBoxString TextBoxStringProvider()
{
return this;
}

}
}

———————————————————————–

3. Consumer web part StringConsumer.cs

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace ConnectedWebPartSolution.Webparts.StringConsumer
{

public class StringConsumer : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @”~/_CONTROLTEMPLATES/ConnectedWebPartSolution.Webparts/StringConsumer/StringConsumerUserControl.ascx”;
private ITextBoxString _myProvider;
private Label myLabel;

protected override void OnPreRender(EventArgs e)
{
EnsureChildControls();
if (_myProvider != null)
{
myLabel.Text = _myProvider.TextBoxString;
}
}

protected override void CreateChildControls()
{
Controls.Clear();
myLabel = new Label();
myLabel.Text = “Default text”;
Controls.Add(myLabel);
}

[ConnectionConsumer(“String Consumer”)]
public void TextBoxStringConsumer(ITextBoxString Provider)
{
_myProvider = Provider;
}

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s