Can't call alert from Server Side

463 views Asked by At

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);
                            }
2

There are 2 answers

0
Lahwary On BEST ANSWER

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()" />
3
beppe9000 On

You need to register the full script, i believe

have you seen the example here? http://msdn.microsoft.com/it-it/library/bahh2fef%28v=vs.110%29.aspx