【发布时间】:2016-05-01 13:07:13
【问题描述】:
供您参考(这是我最初的问题webforms : add dynamically in javascript option to a dropdownlist,感谢 ConnorsFan 解决)。
我的目标是拥有一个支持多选的基础设施下拉列表,并且在每次选择时我都希望在服务器端触发一个事件而不刷新整个页面。
这是我的 aspx 页面:
<%@ Register assembly="Infragistics45.Web.v16.1, Version=16.1.20161.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" namespace="Infragistics.Web.UI.ListControls" tagprefix="ig" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<ig:WebDropDown ID="WebDropDown1" runat="server" Width="200px" OnSelectionChanged="WebDropDown1_SelectionChanged" EnableMultipleSelection="true" EnableClosingDropDownOnSelect="false" AutoPostBack="true">
</ig:WebDropDown>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlId="WebDropDown1" EventName="SelectionChanged"/>
</Triggers>
</asp:UpdatePanel>
这是我的代码隐藏页面:
private List<string> allPossiblechoices = new List<string>() { "a", "b", "c","d","e" };
private List<string> defaultChoices = new List<string>() { "a", "b", "c" };
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
foreach(var choice in allPossiblechoices)
{
WebDropDown1.Items.Add(
new DropDownItem()
{
Text = choice,
Value = choice,
Selected = defaultChoices.Contains(choice)
}
);
}
}
}
protected void WebDropDown1_SelectionChanged(object sender, DropDownSelectionChangedEventArgs e)
{
// I put a breakpoint here to see what e.NewSelection and e.OldSelection are
}
默认情况下,第一次请求页面时,下拉列表由a,b,c,d,e组成,并且只选择a,b,c。
当我选择 d 时,确实向服务器发送了一个请求(我在我的事件处理程序中放置了一个断点)并且结果是正确的:
EventArgs e.OldSelection 包含 a、b、c。
EventArgs e.NewSelection 包含 a,b,c,d。
然后,我取消选择 d,结果如下:
EventArgs e.OldSelection 包含 a,b,c.d.
EventArgs e.NewSelection 包含 a,b,c,d。
我不明白为什么 EventArgs e.NewSelection 包含 d,即使我取消了它。
更奇怪的是,我在没有 updatePanel 的情况下做了同样的事情,一切正常,选择(新旧)都是正确的。
提前感谢您的帮助。
【问题讨论】:
-
您是否尝试将
WebDropDown1放入更新面板中? -
我不想把它放进去,因为在 webdropdown1 上的每个选择中,它都会被关闭,因此一次进行多选的事实将会丢失(即使使用属性EnableClosingDropDownOnSelect)
-
根据以下文章,该列表将始终在回发时关闭:infragistics.com/community/forums/t/92484.aspx。除非您每次都使用
openDropDown重新打开它(这会导致闪烁效果),否则您可能需要实现您在另一篇文章中首先考虑的客户端代码。目前,WebDropDown 保持人为打开,因为它不包含在部分更新中。 -
这篇文章提供了一些关于如何在 WebDropDown 中添加项目的参考,使用客户端代码:infragistics.com/community/forums/t/90300.aspx。
-
好的,谢谢,我会尝试,但是通过这个解决方案,我不会遇到原来的问题:通过 javascript 添加的元素在进入服务器时不会保留
标签: c# asp.net webforms infragistics