4

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?

2
  • Can you provide more information on what didn't work in the code from the question you referenced? Commented Feb 20, 2012 at 17:41
  • isn't that second parameter supposed to be the name of the updatepanel? maybe I'm thinking of another register script method... Commented Feb 22, 2012 at 7:38

2 Answers 2

8

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

you are adding a script from code behind, I can't see any risk!
Literal lit = new Literal(); is better I think, don't have to wrap a <span></span> in this case.
yes I agree with Bruce, Literal is a better choice, thank you.
2

Use this apporch

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

1 Comment

I get: MyPage.aspx: ASP.NET Runtime Error: Only Content controls are allowed in a page that contains Content controls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.