C# ASP.NET DropDownList AutoPostback 不起作用 - 我错过了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/341080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET DropDownList AutoPostback Not Working - What Am I Missing?
提问by Ben Aston
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.
当所选项目更改时,我试图通过 UpdatePanel 将 DropDownList 获取到 AutoPostBack。对于为什么这不起作用,我有点疯狂。
Does anyone have any quick ideas?
有没有人有任何快速的想法?
ASPX page:
ASPX 页面:
<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.
好的,我现在可以使用了。很奇怪。所需要的只是重新启动 Visual Studio。这是一种让我作为开发人员感到害怕的事情;) 我想我以前见过类似的事情,VS 在它运行的程序集上“不同步”。
FYI I am running VS 2008 Web Developer Express.
仅供参考,我正在运行 VS 2008 Web Developer Express。
Thanks to those that answered.
感谢那些回答。
采纳答案by Programmin Tool
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.
一个问题可能是 Vista 并且没有以管理员身份运行 Visual Studios。我知道有不允许调试的倾向。
也许您正在运行的程序集与代码不匹配?如果您“在浏览器中查看”然后附加调试器,则可能会发生这种情况。
回答by Programmin Tool
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
我也有同样的问题,奇怪的是我的更新面板在 FireFox 中触发了 OnTextChanged 但在 IE 上已经死了。Restartin VS 2005 修复了这个问题。:O
回答by Programmin Tool
Rather than using AutoPostBack="true" set the DropList as a trigger in the update panel.
而不是使用 AutoPostBack="true" 将 DropList 设置为更新面板中的触发器。
回答by ZahidKakar
EnableViewState="true" in UpdatePannel will definitely resolve the problem.
UpdatePanel 中的 EnableViewState="true" 肯定会解决问题。