Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi have quite a weird problem on my hands and I am sure I am just overlooking something quite obvious.

I am trying to bind some values to a drop down list. ViewState is definitely enabled. If I add the values to the ddl like this:

XML
<asp:DropDownList ID="ddlUsersList" runat="server" AutoPostBack="true">
<asp:ListItem Text="Bob" />
<asp:ListItem Text="George" />
</asp:DropDownList>


or like this:
C#
ddlUsersList.Items.Add(new ListItem("Bob", "0"));
ddlUsersList.Items.Add(new ListItem("George", "0"));


I dont have an issue. When I select a new user, after the postback that user is still selected. However, when I bind like this:
C#
IEnumerable<GetUsers_Result> result = context.GetUsers("fred", (int)UserTypes.All, (int)UserStatus.Active, false);
foreach (GetUsers_Result r in result)
        ddlUsersList.Items.Add(new ListItem(r.Surname + ", " + r.Forename, ""));


Whenever I select a new value, after the postback it selects the first result?

Please can someone help me figure this out.

Update: The BindUsers() method is called only once (I have used a break point to check) and it is called on the page load event and like this:

C#
if (!Page.IsPostBack)
    BindUsers();
Posted
Updated 15-Mar-12 3:01am
v2
Comments
shreekar 15-Mar-12 10:43am    
can you use trace.output to see the call stack/ event chain to see if that helps?

1 solution

Where and when is the binding taking place? It isn't strange at all that if you rebind after a postback it will not maintain the selected state.
 
Share this answer
 
Comments
DominicZA 15-Mar-12 8:59am    
My point exactly. I have update the question to explain when the binding takes place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900