C# 在 UpdatePanel 之外更新控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/503662/
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
Updating a control outside the UpdatePanel
提问by Ryan Lundy
So I have a UserControl
with some cascading DropDownList
s on it. Selecting from list 1 enables list 2, which in turn enables list 3. Once you've made a selection in all three lists, you can move to the next page.
所以我有一个UserControl
带有一些级联的DropDownList
s 。从列表 1 中选择会启用列表 2,这反过来又会启用列表 3。在所有三个列表中进行选择后,您可以移至下一页。
The DropDownList
s are all inside an UpdatePanel
. But the "Next Page" button is outside the UpdatePanel
. That button should be disabled until all three lists have a selection, and then it should be enabled again. But since the button is outside the UpdatePanel
, it doesn't update when I make selections. (Edit: The "Next Page" button is on a page that also contains the UserControl
.)
该DropDownList
s为所有内部的UpdatePanel
。但是“下一页”按钮在UpdatePanel
. 该按钮应该被禁用,直到所有三个列表都有一个选择,然后应该再次启用。但是由于按钮在 之外UpdatePanel
,所以当我进行选择时它不会更新。(编辑:“下一页”按钮位于还包含 的页面上UserControl
。)
I know one way to resolve this:
我知道一种解决方法:
var scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(dropDownList1);
scriptManager.RegisterPostBackControl(dropDownList2);
scriptManager.RegisterPostBackControl(dropDownList3);
This ensures a postback when any dropdown list is changed, so that the button can update. But if I do this, I might as simplify by getting rid of the UpdatePanel
in the first place.
这可确保在更改任何下拉列表时回发,以便按钮可以更新。但是如果我这样做,我可能会通过UpdatePanel
首先摆脱 来简化。
Is there another way, through some clever JavaScript or something, that I can update a control outside an UpdatePanel
without having to give up Ajax?
有没有另一种方式,通过一些聪明的 JavaScript 或其他东西,我可以在UpdatePanel
不放弃 Ajax的情况下更新控件之外的控件?
采纳答案by thmsn
Could you not add a update panel around the "next page" and then add a trigger to the dropdownlist updatepanel for triggering the next page update panel?
你不能在“下一页”周围添加一个更新面板,然后在下拉列表更新面板中添加一个触发器来触发下一页更新面板吗?
Just throwing out ideas, without actually having tried it :)
只是抛出想法,没有真正尝试过:)
回答by Adam Lassek
This would ideally be done in javascript, with a server-side validation check in the click event handler.
理想情况下,这将在 javascript 中完成,并在单击事件处理程序中进行服务器端验证检查。
A quick jQuery example:
一个快速的 jQuery 示例:
//assuming the dropdowns all have the css class "cascade"
$('select.cascade').change(function() {
If ($('option:selected',this).length == 3) {
$('input[id$=btnNext]').removeAttr('disabled');
}
});
回答by Jim Petkus
Place an UpdatePanel around the next button and create a trigger for each of the dropdowns so that it fires an async postback. Ex:
在下一个按钮周围放置一个 UpdatePanel 并为每个下拉菜单创建一个触发器,以便它触发异步回发。前任:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="dropDownList1" />
<asp:AsyncPostBackTrigger ControlID="dropDownList2" />
<asp:AsyncPostBackTrigger ControlID="dropDownList3" />
</Triggers>
回答by coder123
you can upddate a control outside the update panel by placing it into update_panel2 and saying update_panel2.update()
.
您可以通过将控件放入 update_panel2 并说update_panel2.update()
.
Note the UpdateMode
of the UpdatePanel must be set to conditional
for this to work.
请注意,UpdateMode
必须将 UpdatePanel 的 设置conditional
为使其工作。