Question

I am using three webparts. Webpart A creates a value when the user clicks on the button in Webpart A. WebPart B uses this value to create a list with items the user can select. WebPart C uses both values to create a string for testing purposes.

WebPart B uses this list to fill a dropdownbox. Whenever the selectedvalue is changed it should update WebPart C. This is the declaration of the control.

_documentTypeList = new DropDownList();
_documentTypeList.AutoPostBack = true;
this.Controls.Add(_documentTypeList);

The problem here is that using

_documentTypeList.AutoPostBack = true;

reloads the complete page and resets the value in WebPart A to null. How do i send WebPart B's new value to WebPart C without losing the value of WebPart A? This all happens on 1 page.

Was it helpful?

Solution

saving session state varibles can be done using:

    public string Name
    {
        get
        {
            return Page.Session["SaveName"] as string;
        }
        set
        {
            Page.Session["SaveName"] = value;
        }
    }

this is a getter/setter to save/get from session state

to use this do the following:

this gets the value from session state (needs to be in onPreRender event to see session state varibles)

txtName.Text = Name;

this sets into session state (can be anywhere you want to save to session state)

Name = txtName.Text;

for int valuse:

cahnge public string Name to public int Name and same goes for anything else like bool values ect.

so it would look like this for int:

    public int Name
    {
        get
        {
            return Page.Session["SaveName"] as int;
        }
        set
        {
            Page.Session["SaveName"] = value;
        }
    }

As others have noted for data to be passed between webpart you should be using the iInterface method:

provide (webpart sending data) consumer (webpart using that data) and interface that is the connection between the webparts:

the above will 100% work and save your values on postback :)

just incase you need this ;) iv posted some links on connections below.

Connectable WebPart Problems......................?

and

Connection between 2 web parts

hope it helps :)

OTHER TIPS

Have you tried if (!Page.IsPostBack) { your code here } ?

Have you tried creating custom web part connection for your purpose? Going through your requirement may be that's the most appropriate option

Previously the web parts where connected with in the following order.

WebPart A => WebPart B
WebPart B => WebPart C
WebPart A => WebPart C

this created an error if i wanted to call both values in WebPart C when i changed the value in WebPart B. Whenever WebPart B's value was changed it was required to reload the page and therfore reloading WebPart A. Because WebPart A had a value which is created on user interaction and not on page load the value was lost. WebPart B and WebPart C seized to function without the value of WebPart A. If you wish to keep the WebPart Connection as described above please check almostSharePointMaster's answer.

Because in my case WebPart A creates a unique login token i'd rather not save it to the page. I'd prefer keeping it withing the WebParts. To do so i changed the connection between them. Instead above connection i used the following connection.

WebPart A => WebPart B => WebPart C

Webpart B saves the value of WebPart A and provides it together with his own value to WebPart C. This is how WebParts B Interface currently looks like.

public interface IWebPartB
{
    string webPartAValue { get; }
    string webPartBValue { get; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top