问题
For your information (this was my original problem webforms : add dynamically in javascript option to a dropdownlist, solved thanks to ConnorsFan).
My goal is to having a infragistics dropdownlist enabling multi selection and at each selection I want an event fired server side without refreshing the whole page.
This is my aspx page :
<%@ 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>
This is my code-behind page :
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
}
By default, when the page is requested for the first time, the dropdown is composed of a,b,c,d,e and only a,b,c are selected.
When I select d, a request is indeed send to the server (I put a breakpoint in my event handler) and the results are correct :
EventArgs e.OldSelection contains a,b,c.
EventArgs e.NewSelection contains a,b,c,d.
Then, I deselect d and the results are the following :
EventArgs e.OldSelection contains a,b,c.d.
EventArgs e.NewSelection contains a,b,c,d.
I don't understand why EventArgs e.NewSelection contains d even if I deselected it.
The fact that it's even more strange, is that I have done the same thing without the updatePanel and everything works fine, the selection (new and old) are correct.
Thanks in advance for your help.
回答1:
You can call the RegisterStartupScript
static method of the ScriptManager class to add some Javascript code to be executed after the event handler has returned. In the code below, I assume that the ID of the UpdatePanel is UpdatePanel1
.
protected void WebDropDown1_SelectionChanged(object sender, DropDownSelectionChangedEventArgs e)
{
WebDropDown wdd = sender as WebDropDown;
string scriptCode = string.Format("document.getElementById('{0}').openDropDown();", wdd.ClientID);
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "WDDScript1", scriptCode, true);
}
If it works, you will probably see the WebDropDown closing/opening when the panel is updated (unfortunately).
来源:https://stackoverflow.com/questions/36967018/webforms-use-infragistics-webdropdown-in-updatepanel