views:

25

answers:

1

I have two drop down list on a page. The first one list projects and the second list users.

The userlist is populated with an object datasourse that pulls a list of users for the selected Project.

Whenever the Project list selection changes the second ddl Userlist always reverts to the first person in the list instead the person that was selected before a new Project was chosen.

I want to be able to select a new project and not have the selected person in the UserList change.

+1  A: 

You'll need to store the Id of the user that is currently selected before you do you databinding. One way would be to handle SelectedIndexChanged on your Project ddl so that you can grab the user id of the selected item in your User ddl then do the binding manually. Once the binding is done, then you can attempt to set the SelectedValue of the ddl to the User Id you had stored.

EDIT: Added an example:

In your aspx:

<asp:DropDownList ID="projectddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="projectddl_SelectedIndexChanged">
    <asp:ListItem Text="Project 1" Value="1" />
    <asp:ListItem Text="Project 2" Value="2" />
    <asp:ListItem Text="Project 3" Value="3" />
</asp:DropDownList>

    <asp:DropDownList ID="usersddl" runat="server">
    </asp:DropDownList>

In your code-behind:

protected void projectddl_SelectedIndexChanged(object sender, EventArgs e)
{
    string currentlySelectedUserId = usersddl.SelectedValue;

    // Do your user databinding here based on project selected

    usersddl.SelectedValue = currentlySelectedUserId;
}
DavidGouge
So which event do I use to get the selected user? and which do I use to set the selected user?
Joshua Slocum
Edited to give example.
DavidGouge
I'm a little confused. the ProjectList has an objectdatasource control. Does it rebind every time you change the selected item?
Joshua Slocum
Not sure. I don't tend to use the ObjectDataSource. I prefer to set the .DataSource in the page load and then call DataBind (on the project list in your case). So if that is wrapped in a !Page.IsPostBack then it will only bind once.
DavidGouge