在C#代码中弹出警报

编程入门 行业动态 更新时间:2024-10-09 20:27:17
本文介绍了在C#代码中弹出警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我试图从文件后面的c#代码显示弹出警报。 我的问题是当弹出语句是唯一要执行的东西时,它会很顺利。但是当它与其他声明结合使用时,它不起作用。 例如, 这是有效的

if (condition) { System.Web.UI.ScriptManager.RegisterStartupScript(此,GetType(), displayalertmessage, alert('要显示的警报');,真); }

但这不是 if (condition) { emp.logout(); System.Web.UI.ScriptManager.RegisterStartupScript( this ,GetType(), displayalertmessage, alert('要显示的警报') ;, true ); }

我尝试了其他各种方法,但它从不起作用与其他代码行一起。 我遗失的任何东西? 我尝试了什么: if(条件) { emp.logout(); System.Web .UI.ScriptManager.RegisterStartupScript(this,GetType(),displayalertmessage,alert('要显示的警报');,true); }

解决方案

您必须了解asp页面生命周期和发生的顺序。首先,您的代码隐藏运行并生成html(所有RegisterStartupScript都会将javascript注入到该html中),即html被发送到浏览器执行,此时你看到弹出窗口。 如果emp.logout将用户重定向到另一个页面然后执行该操作重定向所有的html属你的代码背后的代码被简单地丢弃并替换为告诉浏览器重定向的页面,因此发出警报的js也被丢弃并且永远不会执行。 需要注意的重要一点是代码没有在浏览器中运行,在页面完成之前无法向浏览器发送任何内容,而RegisterStartupScript调用实际上并不执行javascript。

如果你想在重定向之前显示警告,你应该在javascript中执行它

emp.logout(); string redirectScript = window.location .href ='otherPage.aspx';; System.Web.UI.ScriptManager.RegisterStartupScript( this ,GetType(), displayalertmessage, alert('要显示的警报') ; + redirectScript, true );

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),alertMessage,alert('show my alert message'),true);

Hi All, I tried to display pop up alert from c# code behind file. My problem is that when the pop up statement is the only thing to be executed, it goes very well. But when it is combined with some other statement, it does not work. For example, this works

if(condition) { System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');", true); }

but this don't

if (condition) { emp.logout(); System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');", true); }

I have tried various other ways but it never work with other code lines. Anything I am missing? What I have tried: if (condition) { emp.logout(); System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');", true); }

解决方案

You have to understand the asp page lifecycle and the order things occur in. First your code-behind runs and that generates html (all RegisterStartupScript does is inject javascript into that html), that html is sent to the browser to execute, and it is at that point you see the popup. If emp.logout redirects the user to a different page then to do that redirect all of the html generated by your code behind is simply discarded and replaced with a page that tells the browser to redirect, so the js that makes your alert is also discarded and is never executed. The important thing to note is that your code isn't running inside the browser, you can't send anything to the browser before the page is finished, and the RegisterStartupScript call doesn't actually execute javascript.

If you want to show the alert before redirection you shall do it in javascript

emp.logout(); string redirectScript = " window.location.href = 'otherPage.aspx';"; System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "alert('alert to be displayed');" + redirectScript, true);

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('show my alert message')", true);

更多推荐

在C#代码中弹出警报

本文发布于:2023-11-28 01:02:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出   警报   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!