javascriptc#asp.netwebformsclientscript

Can't call alert from Server Side


In Web Forms project i need to open alert.

I try to do it like this

var script = Page.ClientScript;
                            if (!script.IsClientScriptBlockRegistered(this.GetType(), "SignOffAlert"))
                            {
                                script.RegisterClientScriptBlock(this.GetType(), "text", "SignOffAlert");
                            }

and add js-function on view

function SignOffAlert() {
            alert('The form cannot be submitted!');
        }

On Button click alert is absent


Update :

This code works for me

var script = Page.ClientScript;
                            if (!script.IsClientScriptBlockRegistered(this.GetType(), "signOffAlert"))
                            {
                                script.RegisterClientScriptBlock(this.GetType(), "signOffAlert", "alert('The form cannot be submitted!');", true);
                            }

Solution

  • try this, in the Page_Load write this:

    string myscript = "  <script> function SignOffAlert() { alert('The form cannot be submitted!');}</script>";
    
        if (! Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "SignOffAlert"))
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "text", myscript);
        }
    

    and in then Form

    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="SignOffAlert()" />