Bound Drop Down List changes to first item in list
I have two drop down list on a page. The first one lis开发者_StackOverflow中文版t 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.
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;
}
精彩评论