views:

28

answers:

2

While debugging a postback problem with a DropDownList within a Repeater I discovered that even though everything looked like it was set up correctly the selections from the user on the DropDownList were not being restored from the view state.

My DropDownList was defined as

<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" />

VS2008 properties inspector indicates that EnableViewState is True Visible is True as can be seen so it should be enabled. Yet stepping through with the debugger and tracing the Page_load, OnItemDataBound and the actual click_event I was interested in showed that the data wasn't being populated from the view state.

The problem I was experiencing was that the selection made by the user wasn't being picked up by my event handling code and it appeared to be reverting to what had been set up at the start of the page render on the initial page_load (which has the obligatory

if (!this.IsPostBack)

wrapper for populating the repeater and dropdown lists.

Inserting a specific enableViewState="True" into my definition as follows solved the problem.

<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" EnableViewState="True" />

Remember that VS2008 was showing this as True anyway on the properties inspector.

Is this expected behaviour? or have I fluked a solution by forcing a condition through?

I'm asking the question as this seems a little odd and I don't want this code to bite me later by suddenly stopping working.

EDIT to clarify how I am processing the code

I have a few dropdown lists that are generated using a repeater and are all populated from within the OnItemDataBound callback on each loop through the repeater.

I have a button outside the repeater that is hooked up to an onclick event handler. It is this handler which is not reading the correct user selection.

Edit - the viewstate appears to be a red herring

After hours of research and debugging I think the view state is a red herring.

I have a bug in here somewhere that is causing my drop down lists not to retain their selected state on the first postback (caused by a button click), When the form is returned they've lost their selection state (they still have the correct contents).

All subsequent postbacks, using the same button retain the correct selections. All code is the same as far as I can see for all postbacks.

it's as if it isn't merging the request parameters into the view state correctly for the very first postback.

I'm trying to set up a very simple example to dig into this, but it's already lost me nearly a days work so far. I've been looking at merging the data myself but the presence of a repeater in my case is not making things easy.

A: 

Viewstate for a control is enabled if all of the following conditions are met:

  1. The EnableViewState property for the page is set to true.

  2. The EnableViewState property for the control is set to true.

  3. The ViewStateMode property for the control is set to Enabled or inherits the Enabled setting.

you could set AutoPostBack = True if needed, check if you are using UpdatePanel, also Visual Studio 2008 has a service pack that should be installed

Edit:

Here's a code working (EnableViewState is always true when debugging). The user's selected value remains the same across http requests.

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      ddl.DataSource = new List<string> { "1", "2", "3" };
      ddl.DataBind();
   }
}

on .aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
   <body>
      <form id="form1" runat="server">
         <asp:DropDownList ID="ddl" runat="server" EnableViewState="true"/>   
      </form>
   </body>
</html>
Junior Mayhé
cheers, but no joy.
John Guy
had specifically checked point 2) and 3) in addition I'm not using an UpdatePanel. I don't want AutoPostBack for the drop down lists. I didn't have anything specified for the page value. I just added in a Page level declaration and it's broken again. Unfortunately reversing back to what I had previously is also broken again now so I was right to worry and I need to delve into how DropDownLists work because it obviously isn't how I thought they did.
John Guy
thanks for the edit. That's effectively what I have (I generate my dropdownlist in a rpeater because I have a few of them). My event is button click event that then reads the drop down lists to get the user input and they aren't reflecting the selections. Interestingly if I then allow the page to be returned to the user after mu button click handler the selections are all correct. So I'm going to go hunting how to read dropdown lists within a repeater now.
John Guy