0
votes

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?

1

1 Answers

1
votes

I've never really agreed with not using the SharePoint-specific subclass of webpart - The only reason against it would be allowing the web part to run in containers other than SharePoint - that's nice in theory, but I have never worked on a project where there was the slightest chance the web part would be used outside SharePoint.

The issue may also be a bit clouded by the existence of a different sort of web part in SharePoint 2003 which predates .NET 2.0 web parts - that's the one that definitely shouldn't be used.