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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:54:41  来源:igfitidea点击:

Run Javascript function after postback in asp.net

c#javascriptasp.netpostback

提问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_Loadis a common place to include logic like this.

Page_Load是包含这样的逻辑的常见位置。

Alternatively, if you will never need whatever is classed as .hideafter they postback and they are server-side controls, you could always set them to Visible = false.

或者,如果您永远不需要.hide在回发后归类的任何内容,并且它们是服务器端控件,则您始终可以将它们设置为Visible = false.

回答by Emad Mokhtar

I seems you need to show notification to users after postback. please read this articleI created code in code behind to show notification.

我似乎您需要在回发后向用户显示通知。请阅读这篇文章我在后面的代码中创建了代码以显示通知。