+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / Trouble with viewstate in usercontrols with 6.2

Trouble with viewstate in usercontrols with 6.2

3 posts, 1 answered
  1. David
    David avatar
    29 posts
    Registered:
    28 Apr 2009
    22 Nov 2013
    Link to this post

    I am working on building a custom widget and am unable to track viewstate properly. I have tried this in a custom user control and a widget created with Thunder. I originally noticed the issue when trying to use the RadGrid and I was unable to get any sorting or paging to work correctly when using the needdatasource event. I then created the user control below to test viewstate. What I found was that when I put a value in the text box it would populate the viewstate property and the value would be written to my label but if I posted back the page again the value in the viewstate property would be cleared. I did make sure that viewstate was enabled in the properties of the sitefinity page that I loaded the widget onto and also turned off cache just for testing. Please let me know what I need to do in order to get viewstate working.

    CS File

    01.using System;
    02.using System.Collections.Generic;
    03.using System.Linq;
    04.using System.Web;
    05.using System.Web.UI;
    06.using System.Web.UI.WebControls;
    07. 
    08.namespace SitefinityWebApp.Controls
    09.{
    10.    public partial class Test : System.Web.UI.UserControl
    11.    {
    12.        protected void Page_Load(object sender, EventArgs e)
    13.        {
    14.            theVal.Text = MyVal;
    15.        }
    16. 
    17.        protected string MyVal
    18.        {
    19.            get
    20.            {
    21.                if (ViewState["MyVal"] == null)
    22.                    ViewState["MyVal"] = "Viewstate null";
    23.                return (string)ViewState["MyVal"];
    24.            }
    25.            set
    26.            {
    27.                ViewState["MyVal"] = value;
    28.            }
    29.        }
    30. 
    31.        protected void update_Click(object sender, EventArgs e)
    32.        {
    33.            MyVal = newVal.Text;
    34.            theVal.Text = MyVal;
    35.        }
    36.    }
    37.}

    Ascx file

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="SitefinityWebApp.Controls.Test" %>
    <asp:Label ID="theVal" runat="server" Text="Wasn't Set" />
    <br />
    <asp:TextBox ID="newVal" runat="server"  />
    <br />
    <asp:Button ID="update" runat="server" Text="Update" OnClick="update_Click" />
    <br />
    <asp:Button ID="post" runat="server" Text="Post" />

  2. Strahil Strahilov
    Strahil Strahilov avatar
    24 posts
    Registered:
    06 Nov 2014
    22 Nov 2013
    Link to this post
    Hi David,

    We answered to your ticket regarding the same issue. If you find the solution useful you can share it with the community.

    Regards,
    Strahil Strahilov
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. David
    David avatar
    29 posts
    Registered:
    28 Apr 2009
    07 Jan 2014
    Link to this post
    After a bit of troubleshooting we eventually discovered that viewstate does not work in preview mode which is where i was working from. After publishing the page and view the live version everything works. Apparently this is by design. 
    Answered
3 posts, 1 answered