I Wrote a sample webpart which takes the parameter passed from orher webpart and display its value on a label. Here is the code:
[Guid("11a885e9-13e1-4c6e-8045-e5575794ebd8")]
public class DisplayParameter : System.Web.UI.WebControls.WebParts.WebPart
{
protected Label _label = new Label();
private string _message;
[ConnectionConsumer("Parameter to show")]
public void GetWPConnectedProviderInterface(IWebPartField connectProvider)
{
FieldCallback callback = FieldCallback;
connectProvider.GetFieldValue(callback);
}
private void FieldCallback(object fieldValue)
{
_message = (string)fieldValue;
}
protected override void OnPreRender(EventArgs args)
{
if (string.IsNullOrEmpty(_message))
_label.Text = "No Message.";
else
_label.Text = _message;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Controls.Add(_label);
}
}
Then I put the WP on a page and a DataView WP generated with SPD, in order to pass a file name from a Doc library to the WP.
It works ok the first time, but when I select another document, I get the following Exception:
Unable to cast object of type 'Test.Assembly.SharepointProject.DislayParameter' to type 'Microsoft.SharePoint.WebPartPages.WebPart'.
This makes me think ... Do I have to inherit from Microsoft.SharePoint.WebPartPages.WebPart instead of System.Web.UI.WebControls.WebParts.WebPart?, and if so, What are the drawbacks of this, since MSDN and bloggers suggest to use the latter class?