绑定下拉列表更改列表中的第一项 [英] Bound Drop Down List changes to first item in list

查看:113
本文介绍了绑定下拉列表更改列表中的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有两个下拉列表。第一个列表项目和第二个列表用户。



用户列表中填有一个对象数据库,其中列出了所选项目的用户列表。



每当项目列表选择更改第二个ddl用户列表始终还原到列表中的第一个人,而不是在选择新项目之前选择的人。



我想要能够选择一个新的项目,而不是在UserList更改中选定的人。

解决方案

您需要存储Id在您进行数据绑定之前,当前选择的用户。一种方法是在您的Project ddl上处理SelectedIndexChanged,以便您可以在用户ddl中获取所选项目的用户标识,然后手动进行绑定。绑定完成后,您可以尝试将ddl的SelectedValue设置为您存储的用户ID。



编辑:添加了一个示例:



在您的aspx中:

 < asp:DropDownList ID =projectddl =serverAutoPostBack =trueOnSelectedIndexChanged =projectddl_SelectedIndexChanged> 
< asp:ListItem Text =Project 1Value =1/>
< asp:ListItem Text =Project 2Value =2/>
< asp:ListItem Text =Project 3Value =3/>
< / asp:DropDownList>

< asp:DropDownList ID =usersddlrunat =server>
< / asp:DropDownList>

在代码隐藏中:

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

//根据项目选择您的用户数据绑定

usersddl.SelectedValue = CurrentlySelectedUserId;
}


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.

解决方案

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;
}

这篇关于绑定下拉列表更改列表中的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆