17

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.

4 Answers 4

9

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 :)

3
  • I managed to fix it on a different way but this is still a great solution to my problem. Thank You! Commented Dec 19, 2012 at 13:13
  • Why are you enabling the viewstate if you are talking about sessionstate before?
    – Norbert
    Commented Dec 19, 2012 at 13:28
  • yup your right I dont know why iv added that in, well spotted :P
    – Ali Jafer
    Commented Dec 19, 2012 at 14:09
7

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

0
0

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

5
  • All three webparts are currently using custom Web Part Connections. They are connected as follows. A -> B, A -> C, B -> C. Changing the value in WebPart A doesn't have any consequences for B and C. But changing the Value of WebPart B and using the _documentTypeList.AutoPostBack = true; it refreshes the page recreating all webparts (thus removing WebPart A's Value). Commented Dec 19, 2012 at 10:56
  • do you really need the auto postback? i can not realize your exact scenario, if you see that it's absolutely necessary then you may want to save the required value in viewstate
    – Diptarag
    Commented Dec 19, 2012 at 11:00
  • I don't need the auto postback. I'd be happy to use any other way to get it to work. I can expand on the scenario if that makes it any easier. Commented Dec 19, 2012 at 11:04
  • if you can post your details scenario, that would be helpful
    – Diptarag
    Commented Dec 19, 2012 at 11:10
  • I am creating one webpart which login on a webservice (WebPart A) the value is a unique ticket. The Second webpart creates a dropdownlist with every Document Type available (WebPart B) the document types are returned by a second webservice. The second webservice requires the unique ticket. The third webpart currently only shows both values but will later be expanded to show all docmetadata from the selected Document Type. The docmetadata also gets returned from the second webservice and requires both the unique ticket and a document type. Commented Dec 19, 2012 at 11:19
0

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; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.