如何判断用户是否更改了下拉列表

发布于 2025-01-05 08:33:25 字数 833 浏览 4 评论 0原文

我有一个 DropDownList,填充它的方法如下:

ddlStaff.Items.Clear();
ddlStaff.Items.AddRange(staff.Select(x => new ListItem(x.Name, x.Id.ToString())
  { Selected = (x == DefaultStaff) }).ToArray());

ddlStaff 位于 UpdatePanel 中,还有另一个 DropDownList,其 SelectedIndexChangedUpdatePanel 的 AsyncPostBackTrigger。该事件更改范围,重新填充 staff 列表,然后再次运行上述内容。

一切都按原样工作正常,但我想做的是知道用户是否更改了 ddlStaff,以便我基本上可以执行类似 { Selected = (x == (UserSelectedStaff ?? DefaultStaff)) }.有内置的方法可以做到这一点吗?或者我只需要记住我发出的最后选择的人员是什么(在会话或视图状态中),然后在返回时将其与实际选择的人员进行比较?

编辑以澄清:我的意图是我可以将 ddlStaff 发送出去,其中填充了工作人员,并且已经选择了默认工作人员。如果用户选择一名工作人员,然后更改更高级别的范围下拉列表,我希望回发不会覆盖用户选择的工作人员(当然,除非该工作人员不存在于新范围内)。但是,如果用户没有更改工作人员,我应该使用新范围的默认值覆盖它。

I have a DropDownList and my method to populate it works as follows:

ddlStaff.Items.Clear();
ddlStaff.Items.AddRange(staff.Select(x => new ListItem(x.Name, x.Id.ToString())
  { Selected = (x == DefaultStaff) }).ToArray());

ddlStaff is in an UpdatePanel, and there's another DropDownList whose SelectedIndexChanged is an AsyncPostBackTrigger for the UpdatePanel. The event changes the scope, repopulates the staff List, then runs the above again.

Everything's working fine as is, but what I'd like to do is know if the user has changed ddlStaff so that I can basically do something like { Selected = (x == (UserSelectedStaff ?? DefaultStaff)) }. Is there a built in way to do this? Or do I just need to remember what the last selected staff I sent out was (in the session or viewstate) and then compare it the actual selected staff when it comes back?

Edit to clarify: My intention is that I can send ddlStaff out populated with staff, and with the default staffmember already selected. If the user selects a staffmember, and then changes the higher-level scope dropdown, I'd like for the postback to not overwrite the user-chosen staffmember (unless of course that staffmember doesn't exist within the new scope). If the user hasn't changed the staffmember, however, I should overwrite it with the default for the new scope.

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

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

发布评论

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

评论(2

℡Ms空城旧梦 2025-01-12 08:33:25

让你的下拉列表“autopostback = True”,并在你的页面加载事件检查

if (!IsPostBack)
    {
      //now you know user made a change

    }

make your dropdownlist "autopostback=True" ,and in your page load Event check

if (!IsPostBack)
    {
      //now you know user made a change

    }
七度光 2025-01-12 08:33:25

一旦响应发送到客户端,服务器上就没有状态,所以我相信你不能真正执行 (x == UserSelectedStaff);在您使用更新面板的场景中,除了某些状态机制之外,我没有看到其他方法,ViewState 可能是基于 DefaultStaff 对象相当轻量级的。

Once the response is sent out to the client there is no state on the server so I believe you cant really do a (x == UserSelectedStaff); in your scenario with update panels I see no other way than some of a state mechanism, ViewState is probably on the bases that DefaultStaff object is fairly lightweight.

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