ASP.NET - 如何使用 C# 显示 javascript 警报?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9365644/
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-08-09 07:02:49  来源:igfitidea点击:

ASP.NET - How to display javascript alert using C#?

c#javascriptasp.net

提问by Theomax

I have a page that contains a textbox and button, when the user clicks the submit button I want to display a message based on a bool value.

我有一个包含文本框和按钮的页面,当用户单击提交按钮时,我想显示基于布尔值的消息。

I have researched on stackoverflow and tried the code in this questions: Asp.net Webform Display Alert and redirect

我研究了 stackoverflow 并尝试了以下问题中的代码:Asp.net Webform Display Alert and redirect

But it didn't work. I have the following code:

但它没有用。我有以下代码:

ClientScript.RegisterStartupScript(this.GetType(),"", "alert('message')", true);

What code do I need to display the alert message?

我需要什么代码才能显示警报消息?

采纳答案by enricoariel

you can use this simple method:

你可以使用这个简单的方法:

    private void MessageBox(string msg)
{
    Label lbl = new Label();
    lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
    Page.Controls.Add(lbl);
}

回答by Deepakmahajan

Use this apporch

使用这个 apporch

 Response.Write(@"<script language='javascript'>alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");