0

LoadClientScriptMessage function doesn't work while Response.Redirect("SamePage.aspx"); works well!

if (command2.ExecuteNonQuery() == 0)
{
     LoadClientScriptMessage("Something went wrong!");
}
else
{
     LoadClientScriptMessage("It is OK!");
     Response.Redirect("SamePage.aspx");
}

Here is the implementation of LoadClientScriptMessage:

private void LoadClientScriptMessage(string message)
{
    StringBuilder script = new StringBuilder();

    script.Append(@"<script language='javascript'>");
    script.Append(@"alert('" + message + "');");
    script.Append(@"</script>");

    Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", script.ToString());
}
2
  • 1
    You want to show alert & redirect the user and complaining that it doesn't work ? Commented Nov 28, 2013 at 15:31
  • Or is it the first call when if condition evaluates to true? Commented Nov 28, 2013 at 15:33

3 Answers 3

3

When you call Response.Redirect("SamePage.aspx") will actually redirect the current request and you will not see any response rendered for the current request.

All you get here is a new page. If you still want to see the alert and redirect then remove Response.Redirect and redirect using javascript window.location.href

 LoadClientScriptMessage("It is OK!");
 //Response.Redirect("SamePage.aspx");
  Page.ClientScript.RegisterStartupScript(this.GetType(), "redirct", 
  "window.location.href='somepage'",true);
Sign up to request clarification or add additional context in comments.

Comments

0

have you write Ajax.Utility.RegisterTypeForAjax(typeof(Sample)); at pageload? and add in web.config

<system.web>

<httpHandlers>
 <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory,Ajax"/>

 </httpHandlers>
 </system.web>

Comments

0
Response.Write("<script>alert('Alert Message')</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.