Web User Control - Dropdownlist Postback Dataloss - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Web User Control - Dropdownlist Postback Dataloss

  • Web User Control - Dropdownlist Postback Dataloss
  • Hi,

    I want to use a Dropdownlist on a Web User Control (ascx). The problem is when I set the Dropdownlist items manually on postback the items are still their and the onSelectedItemsChange event fires.
    i.e.
    <asp:DropDownList ID="DropDownList1" runat="server"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem Value="1" Text="eins"></asp:ListItem>
        <asp:ListItem Value="2" Text="zwei"></asp:ListItem>
    </asp:DropDownList>


    protected void Page_Load(object sender, EventArgs e)
            
                //comes here
            
     
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            
                //comes here and got the right index
            


    But when I generate the code dynamicaly on postback the dropdownlist list all data inside and the selected index is wrong. Furthermore the onSelectedIndexChange event doesnt fire.

    i.e. 
    <asp:DropDownList ID="DropDownList1" runat="server"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
    </asp:DropDownList>

    protected void Page_Load(object sender, EventArgs e)
            
                if(!IsPostBack)
                
                    for(int i = 0; i < 5; i++)
                    
                        DropDownList1.Items.Add(new ListItem(i + "name", i + "value"));
                    
                
            
     
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            
                //never end up here!
            

    What do I made wrong. I tried a lot and their is no UpdatePanel or anything around (only a div). I tried force viewstate (enable). I also tried with datasource and databind().
    Please help :)

    Thanks

  • Thanks. Thats what I needed. Still new in sitefinity *sight*
  • Hi Sydney,

    You should enable the ViewState property on the Page where your UserControl lives. You can do this from the 'Title & Properties' option inside the Sitefinity backend.

    Regards,
    Daniel
  • ARGH!!! This was bugging me for hours. All the ASP.Net sites where giving me all manner of suggestions from changing viewstate, object properties and other tricks. All to find it was Sitefinity being painful, because I didn't know about that enable viewstate property on the admin page.

    Live and learn, but man... I've got a headache now. :(
  • Hi,

    Come controls might require enable view state. To set is you will need to visit the "Titles and Properties" section of the page where the grid resides. There, under Advanced, you will see an unchecked box for Enable ViewState. I have included a screenshot for your convenience.

    Usually, you can enableViewState for the whole site, for the page, or for your control. The same works in Sitefinity as well. You can enable ViewState for your page only, and set EnableViewState to false for all control that don't need it (set it to true for your control only). This is the only way you can partly enableViewState. Otherwise, even if you disable ViewState on page level and enable it on control level, it won't work, because control inherit the viewState of the page.

    I believe you might find this article interesting:
    http://msdn.microsoft.com/en-us/library/system.web.ui.control.enableviewstate.aspx

    Thank you Daniel for helping Sydney and Lurch to sole their problem.

    Kind regards,
    Stefani Tacheva
    the Telerik team
    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