none
How to share data between Web Parts using ViewState?

    Question

  • Hi there,

    Is it possible to pass data between separate web parts using ViewState? Right now I have 2 very simple web parts. On the first one I create a text box and link button within CreateChildControls, and associate an event for the link button's OnClick. In this method I store the value of the textbox to let's say ViewState["value"] = textbox.text;

    Now on the other webpart, I have label control that I create in the CreateChildControls method. Inside this method I ask

    if(ViewState["value"] != null)
    {
       label1.text = ViewState["value"].toString();
    }

    But as you can imagine... it is not working, the label in the second control never sets its value to what's stored in the ViewState.

    What am I doing wrong?

    Thanks.
    Thursday, August 28, 2008 9:16 PM

Answers

  • It is not recommended to use this approach because you never know the order of rendering of web parts. Instead, its better to use web part connections for this purpose. You can make one web part as Connection Provider and other as Connection Consumer to receive this value and define a static connection between them.

    --
    Madhur

    http://blogs.msdn.com/mahuja | Please mark the replies as answers if they help
    Thursday, August 28, 2008 9:31 PM
  • If you use the System.Web.UI.WebControls.WebParts.WebPart and not the SharePoint WebPart base class then you can create a custom interface for the connection.

    http://msdn.microsoft.com/en-us/magazine/cc188696.aspx

    Posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, August 29, 2008 6:25 PM

All replies

  • It is not recommended to use this approach because you never know the order of rendering of web parts. Instead, its better to use web part connections for this purpose. You can make one web part as Connection Provider and other as Connection Consumer to receive this value and define a static connection between them.

    --
    Madhur

    http://blogs.msdn.com/mahuja | Please mark the replies as answers if they help
    Thursday, August 28, 2008 9:31 PM
  • I was afraid of that... I was just able to connect the two, using the IWebPartField interface, which I think does the job for simple web parts. But doing my research... I read about several ways to do the connection: IWebPartField, Custom Interfaces, CellProvider, RowProvider, etc... Dynamic connections, static connections..etc.

    The first two look simple... but the other two require a lot of work implementing methods that I'm not sure what they do. What's your recommendation? What's the right way to do it?

    Another thing, using IWebPartField it allowed me to connect the webparts dynamically instead of creating a static zone connection in the page... which I think it's cool.

    Thanks.
    Friday, August 29, 2008 5:26 PM
  • If you use the System.Web.UI.WebControls.WebParts.WebPart and not the SharePoint WebPart base class then you can create a custom interface for the connection.

    http://msdn.microsoft.com/en-us/magazine/cc188696.aspx

    Posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, August 29, 2008 6:25 PM