Reputation: 69
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.
Upvotes: 1
Views: 4266
Reputation: 66
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"));
Upvotes: 2
Reputation: 1924
Use ajax
$.ajax({
url: validationUrl,
data: {paramName: paramValue }
}).done(function(data ) {
if (data.error)
alert("error");
});
Upvotes: 0