I was previously using static variables to hold variable data that I want to save between postbacks. I was having problems and found that the data in these variables is lost when the appdomain ends. So I did some research and decided to go with ViewStates:
static Dictionary<string, linkButtonObject> linkButtonDictonary;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["linkButtonDictonary"] != null)
{
linkButtonDictonary = (Dictionary<string, linkButtonObject>)ViewState["linkButtonDictonary"];
}
else
{
linkButtonDictonary = new Dictionary<string, linkButtonObject>();
}
}
And here is the very simple class I use:
[Serializable]
public class linkButtonObject
{
public string storyNumber { get; set; }
public string TaskName { get; set; }
}
I am adding to linkButtonDictionary as a gridview is databound:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = (LinkButton)e.Row.FindControl("taskLinkButton");
linkButtonObject currentRow = new linkButtonObject();
currentRow.storyNumber = e.Row.Cells[3].Text;
currentRow.TaskName = e.Row.Cells[5].Text;
linkButtonDictonary.Add(btn.UniqueID, currentRow);
}
}
It appears that my previous issues are resolved however a new one has arisin. Sometime when I postback I am getting this error:
[A]System.Collections.Generic.Dictionary2[System.String,linkButtonObject] cannot be cast to [B]System.Collections.Generic.Dictionary2[System.String,linkButtonObject]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
I don't understand how there can be a casting issue when I am using the same class everywhere. What am I doing wrong and how do I fix it?