问题
I am attempting to get a DropDownList to AutoPostBack via an UpdatePanel when the selected item is changed. I'm going a little stir-crazy as to why this isn't working.
Does anyone have any quick ideas?
ASPX page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true" >
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>item 1</asp:ListItem>
<asp:ListItem>item 2</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
Code-behind (I put a breakpoint on the string assignment to capture the postback):
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = "";
}
Edit:
OK, I have it working now. Very weird. All it took was a restart of Visual Studio. This is the kind of thing that frightens me as a developer ;) I think I've seen similar before, where VS gets "out of sync" wrt the assembly it's running.
FYI I am running VS 2008 Web Developer Express.
Thanks to those that answered.
回答1:
I was able to get it to work with what you posted. This is the code I used... Basically what you had but I am throwing an exception.
<asp:ScriptManager ID="smMain" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true" >
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>item 1</asp:ListItem>
<asp:ListItem>item 2</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
I tried a lot of variations to see if there was something off, but the exception was thrown every time.
You might want to try the exception route to see if the postback is happening and this isn't a debugger issue.
One issue might be with Vista and not running Visual Studios as administrator. I know that has a tendency to not allow debugging.
Maybe the assembly you are running doesn't match the code? This might happen if you "View in Browswer" and then attach the debugger.
回答2:
EnableViewState="true" in UpdatePannel will definitely resolve the problem.
回答3:
I too had the same issues,strangely enough my updatepanel was firing OnTextChanged in FireFox but was dead on IE. Restartin VS 2005 fixed the issue. :O
回答4:
Rather than using AutoPostBack="true" set the DropList as a trigger in the update panel.
来源:https://stackoverflow.com/questions/341080/asp-net-dropdownlist-autopostback-not-working-what-am-i-missing