Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

like image 567
Theomax Avatar asked Dec 28 '22 05:12

Theomax


2 Answers

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);
}
like image 105
enricoariel Avatar answered Jan 12 '23 21:01

enricoariel


Use this apporch

 Response.Write(@"<script language='javascript'>alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");
like image 35
Deepakmahajan Avatar answered Jan 12 '23 19:01

Deepakmahajan