javascript 在asp.net中回发后运行Javascript函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17325974/
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
Run Javascript function after postback in asp.net
提问by Debomboys
I have a button which causes a postback and also calls the javascript function hideInsert() which looks something like this:
我有一个按钮会导致回发并调用 javascript 函数 hideInsert() ,它看起来像这样:
function hideInsert() {
$('.hide').hide();
alert("hide");
}
All it does is hiding tablerows marked with ".hide". This works as intended but since the postback occurs, everything gets reset.
它所做的只是隐藏标有“.hide”的表格。这按预期工作,但由于发生回发,一切都会重置。
Is there anyway I can click the button to trigger the postback and then run the function, after the postback has occurred?
无论如何,我可以单击按钮触发回发,然后在回发发生后运行该功能吗?
I have been looking at this http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspxbut with no success.
我一直在看这个http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx但没有成功。
I would then need to press another button which would trigger the showInsert() function, which would need a similar function.
然后我需要按下另一个按钮来触发 showInsert() 函数,这需要类似的函数。
回答by Jaime Torres
In whatever event makes most sense according to your current architecture, include:
在根据您当前的架构最有意义的任何事件中,包括:
if (Page.IsPostBack) {
ClientScript.RegisterStartupScript(this.GetType(), "HideOnPostback", "$(function() { hideInsert(); })", true);
}
Page_Load
is a common place to include logic like this.
Page_Load
是包含这样的逻辑的常见位置。
Alternatively, if you will never need whatever is classed as .hide
after they postback and they are server-side controls, you could always set them to Visible = false
.
或者,如果您永远不需要.hide
在回发后归类的任何内容,并且它们是服务器端控件,则您始终可以将它们设置为Visible = false
.