I have a simple ASP application page added in a SharePoint project just for presentation purposes, so it's ASP web forms page is hosted in SharePoint.
HTML:
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label runat="server">Number 1:</asp:Label>
<asp:TextBox ID="num1" runat="server"></asp:TextBox>
<asp:Label runat="server">Number 2:</asp:Label>
<asp:TextBox ID="num2" runat="server"></asp:TextBox>
<asp:Label runat="server">Result:</asp:Label>
<asp:TextBox ID="res" runat="server"></asp:TextBox>
<asp:Button Text="ADD Numbers" runat="server" OnClick="Unnamed_Click" />
<asp:ListBox runat="server" ID="list" />
<asp:Label runat="server" ID="previousListValue"></asp:Label>
<asp:Label runat="server">Exception:</asp:Label>
<asp:TextBox ID="exception" runat="server"></asp:TextBox>
</asp:Content>
Here is code behind:
public partial class Default : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
list.DataSource = new List<ListItem>() {
new ListItem("value1", "1"),
new ListItem("value2", "2"),
new ListItem("value3", "3"),
};
list.DataBind();
}
}
protected void Unnamed_Click(object sender, EventArgs e)
{
try
{
res.Text = Convert.ToString(int.Parse(num1.Text) + int.Parse(num2.Text));
previousListValue.Text = "Previous list selected value is: " + list.SelectedItem.Value;
exception.Text = string.Empty;
}
catch (Exception ex)
{
exception.Text = ex.GetType().ToString() + "\t" + ex.Message;
}
}
}
When you click "ADD Numbers" button and addition of num1
and num2
textboxes is appended to the res
textbox and selected value of ListBox
is shown in label next to it. This works flawlessly if correct values are entered for a number and an item is selected in ListBox
.
If the page is left (not interacted with) for about 10-15 minutes and "ADD Numbers" button is clicked, NullReferenceException
will be thrown for list.SelectedItem.Value
and also textBoxes will be empty. What happened is that the application is in postBack state(Page.IsPostBack
is true) but viewstate is not restored.
I guess this has something to do with ViewState being moved to distributed cache as of version 2013 but can someone clarify this for me, and suggest me the most effective way to go about this, without changing AppFabric configuration, because 10-15 minutes of ViewState perversion is not acceptable.