绑定下拉列表更改为列表中的第一项

发布于 2024-09-16 20:47:05 字数 177 浏览 3 评论 0原文

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

用户列表中填充有对象数据源,该数据源提取所选项目的用户列表。

每当项目列表选择更改时,第二个 ddl 用户列表始终恢复为列表中的第一个人,而不是选择新项目之前选择的人员。

我希望能够选择一个新项目,并且不会更改用户列表中选定的人员。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-09-23 20:47:05

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

编辑:添加了一个示例:

在您的 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>

在您的代码隐藏中:

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

    // Do your user databinding here based on project selected

    usersddl.SelectedValue = currentlySelectedUserId;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文