javascript 如何在 C# Asp.net 中注销 Page.ClientScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14337214/
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
How to unregister Page.ClientScript in C# Asp.net
提问by Learning Curve
I am registering java script to my Asp.net code behind file, which is working fine. Now, I have some update panels on the same page and problem is whenever there is any change in any of the update panel, this script is automatically getting called. Is there any way that I can stop this happening. I can't remove update panels from my page and this script is also a very essential part of the application. In this situation I am just calling a alert (rad alert with set time out functionality) when Save Button is clicked or an Update routine is called while I have few other buttons in update panels and whenver any of the button which is registered to the update panels clicked, the following script is called un-willingly. Anyone's help will really be appreciated.
我正在将 java 脚本注册到我的 Asp.net 代码隐藏文件中,它工作正常。现在,我在同一页面上有一些更新面板,问题是只要任何更新面板有任何更改,都会自动调用此脚本。有什么办法可以阻止这种情况发生。我无法从我的页面中删除更新面板,这个脚本也是应用程序的一个非常重要的部分。在这种情况下,我只是在单击“保存”按钮或调用“更新”例程时调用警报(具有设置超时功能的警报),而我在更新面板中几乎没有其他按钮,并且无论何时注册到更新的任何按钮单击面板,不情愿地调用以下脚本。任何人的帮助将不胜感激。
following is my Page.ClientScript
以下是我的 Page.ClientScript
string radalertscript = "<script language='javascript'> Sys.Application.add_load(function(sender, e) {var oWnd = radalert('dialogMessage', 400, 140, 'Saved');window.setTimeout(function () { oWnd.Close(); }, 3000);});</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);
回答by Adil
You can assign empty string to same key radalert
to remove the script.
您可以将空字符串分配给相同的键radalert
以删除脚本。
if(some_condition)
Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", "");
Edit:Based on comments, you can make it simple without using RegisterStartupScript
编辑:根据评论,您可以在不使用的情况下使其变得简单RegisterStartupScript
In code behind
在后面的代码中
btnSave.Attributes.Add("", "saveButtonFunction();");
In Javascript
在 JavaScript 中
<script language='javascript'>
Sys.Application.add_load(function(sender, e) {
if(btnSaveClicked){
var oWnd = radalert('dialogMessage', 400,140, 'Saved');
window.setTimeout(function () { oWnd.Close(); }, 3000);
btnSaveClicked = false;
}
});
btnSaveClicked = false;
function saveButtonFunction(){
btnSaveClicked = true;
};
</script>
回答by Learning Curve
Thank you very much for your answer Adil. I already have followed the same approach with little difference. I have taken JavaScript out from my code behind file and have registered Sys.Application.add_load event as follow
非常感谢您的回答阿迪尔。我已经遵循了相同的方法,几乎没有区别。我已经从我的代码隐藏文件中取出 JavaScript 并注册了 Sys.Application.add_load 事件如下
Sys.Application.add_load(DisplayRadAlertHandler);
function DisplayRadAlertHandler() {
var getMessage = document.getElementById('<%=radAlertDialogHidden.ClientID%>').value;
if (getMessage != "") {
document.getElementById('<%=radAlertDialogHidden.ClientID%>').value = "";
var oWnd = radalert(getMessage, 400, 140, 'Saved');
window.setTimeout(function () { oWnd.Close(); }, 3000);
}
}
Here I am setting my alert message in a hidden input field from code behind file and in the above event handler I am just checking if message is there than reset the hidden field and display the message. Your approach is also right and I have marked your answer but as I am displaying my message from multiple locations (Save button, Update routine etc.) so by assigning value to hidden input field and than resetting in above event handler looks more appropriate. Thanks once again for your help.
在这里,我在文件隐藏代码的隐藏输入字段中设置警报消息,在上面的事件处理程序中,我只是检查消息是否存在,而不是重置隐藏字段并显示消息。您的方法也是正确的,我已经标记了您的答案,但是当我从多个位置(保存按钮、更新例程等)显示我的消息时,因此通过为隐藏输入字段分配值而不是在上述事件处理程序中重置看起来更合适。再次感谢您的帮助。