在更新面板的部分回发后执行 javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14038684/
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
Execute javascript after a partial postback of an updatepanel?
提问by Niloo
I have a page that add tree file script to it .
我有一个向其添加树文件脚本的页面。
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/init.js"></script>
<script type="text/javascript" src="js/easing.js"></script>
I have a updatepanel with a dropdownlist. When run SelectedIndexChanged event (partial postback of an updatepanel), don't execute javascript .
我有一个带有下拉列表的更新面板。当运行 SelectedIndexChanged 事件(更新面板的部分回发)时,不要执行 javascript 。
回答by Murali Murugesan
Use the pageLoadfunction:
使用pageLoad函数:
function pageLoad(sender, args) {
InitialiseSettings();
}
function InitialiseSettings(){
// replace your DOM Loaded settings here.
// If you already have document.ready event,
// just take the function part and replace here.
// Not with document.ready
$(element).slideUp(1000, method, callback});
$(element).slideUp({
duration: 1000,
easing: method,
complete: callback});
}
Or, try adding an "end request" event handler with .add_endRequest():
或者,尝试添加一个“结束请求”事件处理程序.add_endRequest():
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InitialiseSettings)
Edit:
编辑:
It would be a better idea for you to move your code from document.readyinto InitialiseSettings(), and to then register it as a pageLoadedevent handler.
最好将代码从document.ready移入InitialiseSettings(),然后将其注册为pageLoaded事件处理程序。
Code Example
代码示例
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitialiseSettings)
回答by Avishek
To run your javascript in full and partial postbacks, put your javascript code into javascript pageLoad() function.
要在全部和部分回发中运行您的 javascript,请将您的 javascript 代码放入 javascript pageLoad() 函数中。
function pageLoad()
{
//your javascript code
}
Example:
例子:
function pageLoad() {
$(':submit').click(function () {
CommodityArray();
});
$('#btn_image').click(function () {
CommodityArray();
});
$(".repHeader").disableSelection();
CommodityArray();
}
Hope it helps! :)
希望能帮助到你!:)
回答by Azhar Mansuri
You have to use following code after your update panel.
您必须在更新面板后使用以下代码。
<script type="text/javascript" language="javascript">
var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
pageRequestManager.add_endRequest(NewCharacterCount);
</script>
where NewCharacterCountis your javascript function name.
NewCharacterCount您的 javascript 函数名称在哪里。
Read this article Sys.WebForms.PageRequestManager endRequest EventHope it may help you.
阅读这篇文章Sys.WebForms.PageRequestManager endRequest 事件希望它可以帮助你。
回答by Rakesh Dhiman
If you are using UpdatePanel and you want to call a javascript function after content refresh in update panel, you can use below way to do it easily. In Page bodytag , call a function RefreshContent()
如果您正在使用 UpdatePanel 并且想要在更新面板中的内容刷新后调用 javascript 函数,您可以使用以下方法轻松完成。在 Pagebody标签中,调用一个函数RefreshContent()
<body onload="RefreshContent()">
<script type="text/javascript">
function RefreshContent()
{
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
function EndRequestHandler()
{
alert("Add your logic here" );
}
</script>
Reference link http://www.infoa2z.com/asp.net/how-to-call-javascript-function-after-an-updatepanel-asychronous-request-to-asp.net-page
回答by Hans Vonn
You can use PageRequestManager client events. The sender parameter will contain the information you need. For example one could do this:
您可以使用PageRequestManager 客户端事件。sender 参数将包含您需要的信息。例如,可以这样做:
// Register event handler
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
/// Executed when all page content is refreshed, full page or async postback: https://msdn.microsoft.com/en-us/library/bb397523.aspx
function pageLoaded(sender, args) {
var isPostBack = sender.get_isInAsyncPostBack();
if(!isPostBack) return;
// PostBack logic here.
}

