Translate

Friday 16 November 2012

Alert Message from CodeBehind

1. How to Redirect a page after clicking OK of javascript alert box.

Way 1:

C# code behind:
string strScript = "<script>";
strScript += "alert('Type your message here');";
strScript += "window.location='TestPage.aspx';";
strScript += "</script>";
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);

Way 2:

 Response.Write("<script>alert('Successfully changed')</script>");
 Response.Write("<script>window.location.href='Default.aspx';</script>");

Way 3:

 ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "'); window.location='newpage.aspx';", true);

2. Alert message from Codebehind:

Way 1:


   ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Success');", true);


Way 2:

  Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('No Records Available');", true);


Way 3:
If we have used Update Panel, Use following code.

 ScriptManager.RegisterStartupScript(this.Page, typeof(UpdatePanel), "key", "alert('Success');", true);

2. Confirmation Message:


<script type="text/javascript">
        function Confirm(id, des) {
            // debugger;
            try {
                if (document.getElementById(id).value != "") {
                    var r = confirm("This is new Code.Do you want add this code?");
                    if (r == true) {
                        document.getElementById(des).value = "";
                        document.getElementById(des).focus();
                        return true;

                    }
                    else {
                        document.getElementById(id).value = "";
                        document.getElementById(id).focus();
                        return true;
                    }
                    return false;
                }
            }
            catch (e) {
            }
        }
     
    </script>