Try Now
More in this section

Forums / Developing with Sitefinity / Viewstate not working

Viewstate not working

4 posts, 0 answered
  1. higgsy
    higgsy avatar
    336 posts
    05 Aug 2010
    15 Dec 2010
    Link to this post

    Im working with a control which is inherriting the EventsView control. I have added a radiobuttonlist and dropdownlist to the control so the user can filter the results.

    My code for populating the radiobuttonlist and dropdownlist with their options is:

    protected override void OnPreRender(EventArgs e) {
        if (!this.Page.IsPostBack) {
            //create xml data source
            XmlDataSource oCountiesXmlDs = new XmlDataSource();
            oCountiesXmlDs.DataFile = "~/lib/xml/Counties.xml";
            oCountiesXmlDs.XPath = "/root/county";
            //bind the dropdown
            this.oCountyDropDownList.DataTextField = "name";
            this.oCountyDropDownList.DataValueField = "name";
            this.oCountyDropDownList.DataSource = oCountiesXmlDs;
            //get event categories and bind to radiobuttonlist
            ContentManager oContentMgr = new ContentManager("Events");
            IList listOfAll = oContentMgr.GetCategories();
            //bind the radiobuttonlist
            this.oCategoryList.DataTextField = "CategoryName";
            this.oCategoryList.DataValueField = "CategoryName";
            this.oCategoryList.DataSource = listOfAll;
        //we do NOT want any quesystrings appended to the url
        this.Page.Form.Action = (this.Context.Request.RawUrl.Contains('?')) ? this.Context.Request.RawUrl.Remove(this.Context.Request.RawUrl.IndexOf('?')) : "";

    The problem is, the dropdown and radiobuttonlist do not maintain their states using ViewState - why not?

    I have also enabled viewstate in the masterpage within the overridden oninit method.

  2. Georgi
    Georgi avatar
    3583 posts
    10 Oct 2016
    16 Dec 2010
    Link to this post
    Hi Higgsy,

    They most probably do not persist in the viewstate because the ListBox and the DropDown list contain objects (ListItem), rather than simple values. Such objects cannot be maintained by the viewstate automatically. You will have to get all values of the list, then put them on the viewstate separated with comma for example. Upon load, the whole string can be loaded and the listbox can be populated with each item in the string.

    Best wishes,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Tony
    Tony avatar
    29 posts
    30 Dec 2010
    05 Dec 2011
    Link to this post
    I am experiencing a similar situation with a radio button list and dropdownlist dynamically bound to a datasource. I have a custom control that maintains the viewstate and selected item of a radio button list when the control is used on a standard .aspx page. however once the control is dropped into a Sitefinity page the control loses its items as well as the selected item. Is this a limitation of Sitefinity? If so I think it is a HUGE limitation and should probably be addressed ASAP. The viewstate is a fundamental feature of .NET, Sitefinty should not be limiting basic features of the language. Is there a work around? Any code samples available?

  4. Tony
    Tony avatar
    29 posts
    30 Dec 2010
    05 Dec 2011
    Link to this post
    I stumbled upon another forum post regarding the same issue and saw that there is a check box to enable/disable the viewstate on a Sitefinity page. After enabling the view state via the UI things worked as intended. This is a very strange setting that is required. Why would anybody want to deliberately disable a fundamental feature of the .NET framework?

    Either way my issue appears to be resolved.

Register for webinar
4 posts, 0 answered