如何判断用户是否更改了下拉列表
我有一个 DropDownList
,填充它的方法如下:
ddlStaff.Items.Clear();
ddlStaff.Items.AddRange(staff.Select(x => new ListItem(x.Name, x.Id.ToString())
{ Selected = (x == DefaultStaff) }).ToArray());
ddlStaff 位于 UpdatePanel
中,还有另一个 DropDownList
,其 SelectedIndexChanged
是 UpdatePanel
的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让你的下拉列表“autopostback = True”,并在你的页面加载事件检查
make your dropdownlist "autopostback=True" ,and in your page load Event check
一旦响应发送到客户端,服务器上就没有状态,所以我相信你不能真正执行 (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.