More in this section

Forums / General Discussions / dropdown - autopostback

dropdown - autopostback

4 posts, 0 answered
  1. JV
    JV avatar
    74 posts
    Registered:
    15 Jun 2012
    21 Oct 2010
    Link to this post

    Hi,

    I'm developing my first user control to integrate with Sitefinity 4.0.

     - I have one  dropdown with AutoPostBack=true
    - In page load i bind that dropdown if  IsPostBack=false
    - When i select one value i loose the dropdown state and it becames an empty dropdwon.

    I verified the EnableViewState Property and it's true.

    What am i doing wrong ?

  2. Matt
    Matt avatar
    56 posts
    Registered:
    20 Nov 2009
    22 Oct 2010
    Link to this post
    I'm having similar problems with data bound dropdowns.  I spent the last two days trying to get my first form to save without the lists being emptied during the post back causing validation to fail.  Seems to be related to viewstate, but I tried everything to enable it with no luck.  I decided to try reloading the lists during the postback.  My final solution: bind the lists during the OnInit event.  I usually use the OnLoad but it seems that's too late in the lifecycle.  I only bind the DataSource in the OnInit, and set the SelectedValue only during the full DataBind that happens on load.

    Here's the code that worked for me:

    <asp:DropDownList runat="server" ID="stateList" 
    DataSource="<%# States %>" 
    DataTextField="StateName" DataValueField="StateId"
    />

    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    if (!IsPostBack)
    {
    DataBind();
    }
    }

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    stateList.DataBind();
    }

    public override void DataBind()
    {
    base.DataBind();
    if (!IsPostBack)
    {
    if (!string.IsNullOrEmpty(Model.StateId))
    {
    stateList.SelectedValue = Model.StateId;
    }
    }
    }

    Hope this helps...
    Matt

  3. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    22 Oct 2010
    Link to this post
    Hello Joana and Matt,

    There indeed are problems with the Page's view state and the life cycle. By default the page's view state is not enabled and should be done through code. You can achieve this from the master page:
    protected override void OnInit(EventArgs e)
    {
        this.Page.EnableViewState = true;
        base.OnInit(e);
    }

    We are working on configuration screens which will allow you to set this from the UI, as well as other page properties.

    Kind regards,
    Radoslav Georgiev
    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
  4. JV
    JV avatar
    74 posts
    Registered:
    15 Jun 2012
    22 Oct 2010
    Link to this post
    it worked.

    thanks
4 posts, 0 answered