In this tutorial, we will learn how to create and use an AJAX Confirm Button Extender using C#. This button can be used to warn people of what a button does once they press it. This control is used in lots of websites and it is easy to setup and use.

Step 1

Let's we create the form:

1. Start by Creating a new Web Form and naming it “Default.aspx”
2. Create a Button and a Label on the form named “Button1″ and “Label1″
3. Add an AJAX Script Manager to the Form so that you can use AJAX Controls:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <br />
                <br />
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                <asp:ConfirmButtonExtender ID="Button1_ConfirmButtonExtender" runat="server"
                    ConfirmText="Click OK to make the Label say OK or click Cancel to Cancel the Operation"
                    Enabled="True" TargetControlID="Button1">
                </asp:ConfirmButtonExtender>
            </ContentTemplate>
        </asp:UpdatePanel>
   
    </div>
    </form>
</body>
</html>


4. Add the Confirm Button Extender to the button by clicking the Smart tag and selecting Add Extender
5. Now the Web Form is complete, but it is not ready to be run yet. You must create the code behind.

Step 2

1. Double click on the Button Control in the Design View to open up the Code window.
2. Enter the Code Below, this code would normally make the Label say OK every time you click the button, however the code in the front end only lets it run once you confirm.
3. Once this code is in you can go ahead and run the project and see how it works:

protected void Button1_Click(object sender, EventArgs e)
        {
           
            Label1.Text = "you clicked OK";
        }

Happy coding!

HostForLIFE.eu ASP.NET 4.6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.