Call JavaScript Confirm Box from Code-behind in

In this post, i will exaplain how to call javascript cofirm message box from code-behind page or server-side code.

We use JavaScript confirm box, when we want to notify or varify before doing any action like want to ask before removing records, befor ask payment confirmation.
The javaScript confirm box give user to one chance to varify or confirm action before making any action on web page.

Generally, we use javascript function on button click event using OnClientClick property of button control.
Just write your javascript function name inn OnClientClick property of Button Control to call javascript.

In this tutorials we will lean to call javascript function in conde-behind page uing c# language and language.

Here, the code for javascript confirm box function:

<script type="text/javascript">
    function myconfirmbox()
        if (confirm("Are You Sure, want to do this ?"))
            alert("Thank You!"); //Click OK
            alert("See you later."); //Click CANCEL

Write above javascript confirm box code in Head tag section.

Now, call the myconfirmbox javascript function from code-behind, create a web application and design a web page with one server side button control.

Belwo, is the code of server side button control in, we need to call “btnconfirm_Click” event for call javascript confirm box function.

<asp:Button ID=”btnconfirm” runat=”server” Text=”Call Confirm Box” OnClick=”btnconfirm_Click” /> Code for call javascript function from code-behind in

protected void btnconfirm_Click(object sender, EventArgs e)

ScriptManager.RegisterStartupScript(this, this.GetType(), “myconfirmbox”, “myconfirmbox();”, true);

} Code for call javascript function from code-behind in

Protected Sub btnconfirm_Click(ByVal sender As Object, ByVal e As EventArgs)

ScriptManager.RegisterStartupScript(Me, Me.GetType(), “myconfirmbox”, “myconfirmbox();”, True)

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *