Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > CMS.GlobalHelper.RequestStockHelper.GetItem or ViewState not working on webpart. Why ? View modes: 
User avatar
Member
Member
layek.suman-gmail - 3/22/2013 6:56:06 AM
   
CMS.GlobalHelper.RequestStockHelper.GetItem or ViewState not working on webpart. Why ?
Hello,

I am trying to set value to state aginst a button click and get a value from another button click. It is just shocking that I was unable to meet this reqirement when creating custom webpart.I have seen many people are getting issues with viewstate. Can you please look into the below code and please let me know how can I make this working. Here I am assigning values on btnNext button click and trying to get values on btnPrev button click. Please assist me. I am using Kentico version 7.0
public partial class Customwebpart: CMSAbstractWebPart
{

protected void btnPrev_Click(object sender, EventArgs e)
{
var viewstatevalue = ViewState["Step"] ; //getting null here
var requesthelpervalue = getWord();
}
protected void btnNext_Click(object sender, EventArgs e)
{

ViewState["Step"] = Convert.ToInt32( ViewState["Step"] )+ 2;
setWord("Hello");
}

public string setWord(object input)
{
string word = ValidationHelper.GetString(input, String.Empty);
CMS.GlobalHelper.RequestStockHelper.Add("value", word);
return "";
}

public string getWord()
{
string word = ValidationHelper.GetString(CMS.GlobalHelper.RequestStockHelper.GetItem("value"), String.Empty);
return word;
}

}

User avatar
Certified Developer 13
Certified Developer 13
kentico_josefd - 4/3/2013 9:24:26 AM
   
RE:CMS.GlobalHelper.RequestStockHelper.GetItem or ViewState not working on webpart. Why ?
Hello,

I have tested your code but the storing and retrieving values from ViewState seems to be working for me without issues.

However, I did find the problem with storing and retrieving values using RequestStockHelper. The reason it does not seem to be working is that this helper stores the data in HttpContext.Current.Items, which is cleared after each request / refresh. This is a standard .Net behaviour, as HttpContext serves only for single request. You can find more information in the following articles on HttpContext and Per-request cache.

Depending on what you are trying to achieve, you should be able to use SessionHelper instead, which will store the value for the entire session.

Regards,
Josef Dvorak