very simple way

string msg = "Please recheck time of meal";
                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        sb.Append(@"<script language='javascript'>");
                        sb.Append(@"alert('" + msg + "');");
                        sb.Append(@"</script>");

                        if (!ClientScript.IsStartupScriptRegistered("JSScript"))
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
                        }


Second way if you r using the update panel then use this and pass the name of the your update panel at the place of this.getType()

string msg = "Please recheck time of meal";
            ScriptManager.RegisterStartupScript(this.myFoodInfo, typeof(string), "ShowMessage", "showMSG('" + msg + "');", true);
this is the way to call the javascript method from code behind.

note: here myFoodInfo is the name of the update panel i am using.

if you are not using the update panel then use this method
          string script1 = "<script language=JavaScript>";
            script1 += "alert('Hi, GM');";
           script1 += "</script>";

           Page.RegisterStartupScript("clientscript", script1);

Comments (0)