0

How would I add a javascript MessageBox in ASP.NET in the code behind pages using C#.

I have something like this in mind although I still cant get it quite right.

if (success == false)
    {
        HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert('User name already exists')</SCRIPT>");

    }

Kind regards

5 Answers 5

2

Check this out:
http://msdn.microsoft.com/en-us/library/aa478975.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

Below link will be helpful

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx

http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx

Comments

1

Add a HiddenField to your ASPX

<asp:HiddenField runat="server" ID="errorHf" />

Add the following JS code to your ASPX page

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {
        var sErrorData = $('#errorHf').val();
        if (sErrorData != "") {
            alert(sErrorData);
            $('#errorHf').val("");
        }
    });
</script>

Once you get an error, just set errorHf.Value

if (!success)
    errorHf.Value = "Something went wrong!";

Don't forget to reset the error message on each Page_Load, so add

errorHf.Value = String.Empty;

So the message won't persist with each Postback.

Comments

1

Try this:

<script type="text/JavaScript">
alert('hello world');
</script>

You can use the ASP.NET codebehind to write it to the page

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);

Comments

1

This works for me:

    Dim script As String =
     "<script language='javascript'>" &
     "alert('my message');" &
     "</script>"
    Dim cs As ClientScriptManager = Me.Page.ClientScript
    cs.RegisterStartupScript(GetType(String), "myAlertID", script)

Comments

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.