1

I'm attempting to create an alert that works on the server side, preferably using JavaScript. This alert is displayed if a user's input does not match a value pulled from a SQL server. The client side alert I was using was:

MessageBox.Show("User Input was not found. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

I attempted to replicate this by doing:

   string message = "User Input was not found. Please try again.";
   System.Text.StringBuilder sb = new System.Text.StringBuilder();
   sb.Append("<script type = 'text/javascript' runat = 'server'>");
   sb.Append("window.onload=function(){");
   sb.Append("alert('");
   sb.Append(message);
   sb.Append("')};");
   sb.Append("</script>");
   Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

I also tried this:

   ClientScriptManager cs = Page.ClientScript;
   cs.RegisterClientScriptBlock(
   this.GetType(),
   " ",
   @"<script language=javascript>alert('User Input was not found. Please try again');</script>",
   true
   );

After this alert is displayed, I'd like to immediately redirect using Respone.Redirect("redirectpage.aspx").

Is there a more effective solution to this issue? Am I missing something from my code? This is my first time using JavaScript for server side applications, so any detail would be appreciated.

1
  • what is the issue? i mean what is not working Commented Jul 21, 2014 at 20:15

2 Answers 2

2

Try using this method, it also has parameter to Redirect to Particular page

public class HTMLHelper
    {
        public static void jsAlertAndRedirect(System.Web.UI.Page instance, string Message, string Redirect_URL)
        {
            instance.Response.Write(@"<script language='javascript'>alert('" + Message + "');document.location.href='" + url + "'; </script>");
        }
    }

Call it using this,

HTMLHelper.jsAlertAndRedirect(this.Page, "Success Message !", ResolveUrl("~/default.aspx"));
Sign up to request clarification or add additional context in comments.

Comments

0

Use ajax

$.ajax({
  url: validationUrl,
  data: {paramName: paramValue }
}).done(function(data ) {
  if (data.error)
    alert("error");
});

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.