Viewstate of a DropDownList is lost on postback

This post has 15 replies

Not Ranked
Posts: 2
fregiejj Posted: Mon Jan 7, 2008 @ 9:03 AM
I have a basic dialog with a simple entry form of a couple text boxes and a drop down list with a submit button. On postback the drop down list does not have any items and the selectedIndex is -1. The text boxes's values are there.

I've stripped down my code into the following example code that still reproduces the problem. The code below fills a drop down list with 3 times on page load. Then on postback displays the number of items in the drop down list...which will mysteriously be zero.

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register Assembly="ComponentArt.Web.UI" Namespace="ComponentArt.Web.UI" TagPrefix="ComponentArt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="EN">
<body>
    <form id="form1" method="post" runat="server">
        <script runat="server">
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DropDownList1.Items.Add("one");
                    DropDownList1.Items.Add("two");
                    DropDownList1.Items.Add("three");
                }
            }
            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                lblNumItems.Text = "Num Items: " + DropDownList1.Items.Count;
            }
        </script>
        
        <script>
            function toggle()
            {
    	        if(Dialog1.get_isShowing())
    	        {
    		        Dialog1.Close();
    		        document.getElementById('showclose').value = 'Show Dialog';
    	        }
    	        else
    	        {
    	          Dialog1.Show();
    		        document.getElementById('showclose').value = 'Close Dialog';
    	        }
            }
        </script>

        <input type="button" onClick="toggle();" id="showclose" Value="Show Dialog" />
        <br /><br />
        <asp:Label ID="lblNumItems" runat="server"></asp:Label>

        <ComponentArt:Dialog ID="Dialog1" runat="server">
        <Content>
            <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
            <br /><br />
            <asp:Button id="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        </Content>
        </ComponentArt:Dialog>
    </form>
</body>
</html>


I've experimented with other examples and the viewstate of the drop down list works in some but not in others with no obvious differences in code or the environment. Can anyone explain why this happening? Thanks
Top 10 Contributor
Posts: 6,149
stephen Posted: Thu Jan 10, 2008 @ 1:18 PM
This was addressed in a seperate support request. Here was my response to that request:

I did some testing myself, and I'm seeing the same problem. I've alerted the developers and they'll be looking into this. Until a fix is introduced, there is a workaround - if you add the items every postback, the state of the selected item is maintained. Thus, this works:

protected void Page_Load(object sender, EventArgs e)
{

ListItem li = new ListItem();
li.Text = "one";
li.Value = "1";
DropDownList1.Items.Add(li);

li = new ListItem();
li.Text = "two";
li.Value = "2";
DropDownList1.Items.Add(li);

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblNumItems.Text = "selected: " + DropDownList1.SelectedItem.Text;
}

If the operation adding the items is expensive you might want to do something like create a dataset that has your item data, then bind the dropdown to the dataset. If it is a postabck, bind to the session set.
Stephen Hatcher, Developer Support Manager
Top 500 Contributor
Posts: 20
bcbio Posted: Fri Jun 26, 2009 @ 3:13 PM
Hi,

Has this problem been fixed yet in version Web.UI 2009.
Top 10 Contributor
Posts: 6,149
stephen Posted: Thu Jul 2, 2009 @ 10:51 AM
I just checked our bug tracking software and the issue does unfortunately appear to be still open. I've updated the report to remind the developers that this needs attention, and I do apologize for the trouble.
Stephen Hatcher, Developer Support Manager
Top 500 Contributor
Posts: 20
bcbio Posted: Thu Jul 2, 2009 @ 1:48 PM
The workaround mentioned above does not seem to work for me.

On the first post back, the dropdown item list is blank and selected index is -1.

On the second postback (and all future postbacks), the selected index is correct, however the items are now duplicated because they are added on each postback.

Any other workarounds to get around this problem until a fix is released?

Thanks,
Top 10 Contributor
Posts: 6,149
stephen Posted: Fri Jul 3, 2009 @ 7:49 AM
Hmm.... it's working for me, so we must be doing something differently... Can you post your code here so I can take a closer look? If it's a lot of code, we might be better served in a formal support request so we can more easily exchange code. If that's the case, you can just open a request using the menu above (support/ request support) and, in the request, quote this forum post so I know what it's in regards to.
Stephen Hatcher, Developer Support Manager
Top 500 Contributor
Posts: 20
bcbio Posted: Fri Jul 3, 2009 @ 11:45 AM
Here is the code to populate the dropdown list:

protected void Page_Load(object sender, EventArgs e)
{
	PopulateDropDownValues();
}

private void PopulateDropDownValues()
{
	List<List<string>> paymentTypeList = UserControlUtility.GetPaymentTypes();

	foreach (List<string> currentPaymentType in paymentTypeList)
	{
		ListItem li = new ListItem();
		li.Text = currentPaymentType[0];
		li.Value = currentPaymentType[0];
		
		ddlPaymentType.Items.Add(li);

		ddlPaymentType.Attributes.Add(currentPaymentType[0] + "_id", currentPaymentType[1]);
		ddlPaymentType.Attributes.Add(currentPaymentType[0] + "_currency", currentPaymentType[2]);
	}
}


Some other notes:
- This code is running in a user control.
- The user control is loaded onto a SharePoint page using a SharePoint web part

Thanks,
Top 10 Contributor
Posts: 6,149
stephen Posted: Mon Jul 6, 2009 @ 11:50 AM
SharePoint definitely complicates the problem, as it's not something we currently support... I moved my code into a user control and it functions fine, so it doesn't appear to be that. Can you try this: in page load, determine if you actually need to load the data. For example, check the number of items in the dropdown- if it's zero, you know you need to call PopulateDropDownValues, and if it's not, state should be present, and there's no need to load. Does this make sense?
Stephen Hatcher, Developer Support Manager
Top 500 Contributor
Posts: 20
bcbio Posted: Tue Jul 7, 2009 @ 11:18 AM
I've confirmed that I'm experiencing the same problem in a standalone ASP.net web application (no SharePoint involved). On the 1st postback, the selected value is not saved (it's always -1). On subsequent postbacks the selected value and the item list is correct. Note, the version I'm currently using is: 2009.1.2002.35 Here is the exact code I'm using: You should be able to reproduce the problem with this code. ASPX Page <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridSelectedItem.aspx.cs" Inherits="BCBio.TrialApplication.Web.GridSelectedItem" %> <%@ Register Assembly="ComponentArt.Web.UI" Namespace="ComponentArt.Web.UI" TagPrefix="ComponentArt" %>


Enter Payments
Enter Amount
---CODE BEHIND--- using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ComponentArt.Web.UI; namespace BCBio.TrialApplication.Web { public partial class GridSelectedItem : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (ddlPaymentType.Items.Count < 1) PopulateDropDownValues(); } protected void btnAddPayment_Click(object sender, EventArgs e) { Response.Write(ddlPaymentType.Items.Count); Response.Write(ddlPaymentType.SelectedIndex); } private void PopulateDropDownValues() { List values = new List(){"a","b","c","d","e","f"}; foreach (string currentPaymentType in values) { ListItem li = new ListItem(); li.Text = currentPaymentType; li.Value = currentPaymentType; ddlPaymentType.Items.Add(li); } } } }
Top 10 Contributor
Posts: 6,149
stephen Posted: Tue Jul 7, 2009 @ 1:03 PM
Thanks for posting this, I can recreate with this. Strangely, the problem with the workaround comes from the usage of the input control- removing it entirely fixes the workaround. I'm going to take a closer look at this, as it's not something we're currently aware of, and I'll get back to you asap.
Stephen Hatcher, Developer Support Manager
Top 500 Contributor
Posts: 20
bcbio Posted: Thu Jul 9, 2009 @ 4:55 PM
Hi there,

Any updates on this?

Thanks,
Top 10 Contributor
Posts: 6,149
stephen Posted: Fri Jul 10, 2009 @ 7:17 AM
I'm just waiting to hear back from the dev team for their thoughts. There definitely appears to be a second bug here, one that we weren't aware of, and I'm waiting to hear if they have any insight into where it may be. For now, you can use hidden fields to store the value. So, in your clientclick function on the button, use some js to store the current selected value in a hidden field, and check the value of the hidden field on the server, not the value of the dropdown. I realize this isn't ideal, and I do apologize for that. I'll post back again when I get more information.
Stephen Hatcher, Developer Support Manager
Not Ranked
Posts: 14
romelevans Posted: Wed Jul 21, 2010 @ 3:50 AM
I'm having the same issue.  It's been a while now, 11 days!  Hasn't anyone addressed the fix to this yet?
Top 10 Contributor
Posts: 6,149
stephen Posted: Wed Jul 21, 2010 @ 8:11 AM

This is actually a very old post- over a year old, with a few different scenarios. Which do you have? A dropdownlist in a dialog, a dropdownlist and dialog in sharepoint, a dropdown list in a dialog with a maskedinput control...?  Have you tried, as suggested above, re-adding the items on postback to the dropdown? And can I perhaps see your code so I can try to recreate what you have?

Stephen Hatcher, Developer Support Manager
Not Ranked
Posts: 7
dougalott Posted: Tue Nov 1, 2011 @ 8:37 AM
Is this still a bug?  I am using 2011 WebUI.  I am simply dropping a listbox in a dialog. On postback it completely loses its state.  Adding the items back on each postback is not a solution as we would lose the client's changes to the selected items.
Page 1 of 2 (16 items) 1 2 Next >