在C#代码中弹出警报 [英] Pop up alert in C# code behind

查看:111
本文介绍了在C#代码中弹出警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我试图从文件后面的c#代码显示弹出警报。

我的问题是当弹出语句是唯一要执行的东西时,它会很顺利。但是当它与其他声明结合使用时,它不起作用。



例如,

这是有效的

  if (condition)
{
System.Web.UI.ScriptManager.RegisterStartupScript(,GetType(), displayalertmessage,< span class =code-string> alert('要显示的警报'););
}





但这不是



< pre lang =c#> if (condition)
{
emp.logout();
System.Web.UI.ScriptManager.RegisterStartupScript( this ,GetType(), < span class =code-string> displayalertmessage, alert('要显示的警报') ; true );
}







我尝试了其他各种方法,但它从不起作用与其他代码行一起。

我遗失的任何东西?



我尝试了什么:



if(条件)

{

emp.logout();

System.Web .UI.ScriptManager.RegisterStartupScript(this,GetType(),displayalertmessage,alert('要显示的警报');,true);

}

解决方案

您必须了解asp.net页面生命周期和发生的顺序。首先,您的代码隐藏运行并生成html(所有RegisterStartupScript都会将javascript注入到该html中),即html被发送到浏览器执行,此时你看到弹出窗口。



如果emp.logout将用户重定向到另一个页面然后执行该操作重定向所有的html属你的代码背后的代码被简单地丢弃并替换为告诉浏览器重定向的页面,因此发出警报的js也被丢弃并且永远不会执行。



需要注意的重要一点是.net代码没有在浏览器中运行,在页面完成之前无法向浏览器发送任何内容,而RegisterStartupScript调用实际上并不执行javascript。


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

  emp.logout();  
string redirectScript = window.location .href ='otherPage.aspx';;
System.Web.UI.ScriptManager.RegisterStartupScript( this ,GetType(), < span class =code-string> 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.net 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 .net 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#代码中弹出警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆