AJAX ToggleButton Extender in ASP.Net

ToggleButton Extender

The ToggleButton Extender is an ASP.NET AJAX extender that can be attached to an ASP.NET CheckBox control. ToggleButton allows the use of custom images to show the state of the CheckBox. The behavior of the CheckBox is unaffected by this.
 
ToggleButton Properties 
  • TargetControlID: The ID of the CheckBox to modify.

  • Image Height\Image Width: The height and width of the image.

  • CheckedImageUrl: The URL of the image to show when the toggle button is in the checked state.

  • UncheckedImageUrl: The URL of the image to show when the toggle button is in the unchecked state.
Let's understand it with an example.
 
Step 1: Open Visual Studio and create a ptoject named ToggleButtonExtender.

Step 2
: Add one Webform named ToggleButton.

Step: Now in design view we will add a ToolkitScriptManager.
 
 
 
The source code looks like this: 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ToggleButton.aspx.cs" Inherits="ToggleButtonExtender.ToggleButton" %>  
  2.   
  3. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  16.         </asp:ToolkitScriptManager>  
  17.       
  18.     </div>  
  19.     </form>  
  20. </body>  
  21. </html>  
Step 4: Now right-click in the Solution Explorer and we will add an images folder and put two images inside it. Here we use the smiley images Agree and DisAgree for ToggleButton selection.
 
 
 
Step 5: Now we will add an update panel to the project from AJAX Extensions from the Toolbox.
 
 
 
The source code looks like this.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ToggleButton.aspx.cs" Inherits="ToggleButtonExtender.ToggleButton" %>  
  2.   
  3. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  16.         </asp:ToolkitScriptManager>  
  17.         <br />  
  18.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  19.         </asp:UpdatePanel>  
  20.       
  21.     </div>  
  22.     </form>  
  23. </body>  
  24. </html>  
Step 6: Now inside the content template we will add two check boxes and two Toggle button Extenders to each of them.
 
 
The source code looks like this. 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ToggleButton.aspx.cs" Inherits="ToggleButtonExtender.ToggleButton" %>  
  2.   
  3. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  16.         </asp:ToolkitScriptManager>  
  17.         <br />  
  18.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  19.             <ContentTemplate>  
  20.                 <asp:CheckBox ID="CheckBox1" runat="server" />  
  21.                 <asp:ToggleButtonExtender ID="ToggleButtonExtender1" runat="server"></asp:ToggleButtonExtender>  
  22.   
  23.                 <asp:CheckBox ID="CheckBox2" runat="server" />  
  24.                 <asp:ToggleButtonExtender ID="ToggleButtonExtender2" runat="server"></asp:ToggleButtonExtender>  
  25.             </ContentTemplate>  
  26.         </asp:UpdatePanel>  
  27.       
  28.     </div>  
  29.     </form>  
  30. </body>  
  31. </html>  
Step 7: Now we will provide properties for both ToggleButtonExtender and we will add an ASP button to invoke the extender and a label to display the selected checkbox message by the following code. 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ToggleButton.aspx.cs" Inherits="ToggleButtonExtender.ToggleButton" %>  
  2.   
  3. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  16.         </asp:ToolkitScriptManager>  
  17.         <br />  
  18.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  19.             <ContentTemplate>  
  20.                 <asp:CheckBox ID="CheckBox1" Checked="true" Text='I Agree For Terms And Conditions' runat="server" /><br /><br/>  
  21.                 <asp:ToggleButtonExtender ID="ToggleButtonExtender1" runat="server"  
  22.                     TargetControlID="CheckBox1"  
  23.                     ImageWidth="25"  
  24.                     ImageHeight="25"  
  25.                     UncheckedImageUrl="~/Images/DisAgree.png"  
  26.                     CheckedImageUrl="~/Images/Agree.png"  
  27.                     CheckedImageAlternateText="Check"  
  28.                     UncheckedImageAlternateText="UnCheck">  
  29.                 </asp:ToggleButtonExtender>  
  30.                 <br />  
  31.                 <asp:CheckBox ID="CheckBox2"  Checked="true" Text='I Disagree For Terms And Conditions' runat="server" /><br /><br />  
  32.                 <asp:ToggleButtonExtender ID="ToggleButtonExtender2" runat="server"  
  33.                     TargetControlID="CheckBox2"  
  34.                     ImageWidth="25"  
  35.                     ImageHeight="25"  
  36.                     UncheckedImageUrl="~/Images/DisAgree.png"  
  37.                     CheckedImageUrl="~/Images/Agree.png"  
  38.                     CheckedImageAlternateText="Check"  
  39.                     UncheckedImageAlternateText="UnCheck">  
  40.                 </asp:ToggleButtonExtender>  
  41.                 <br />  
  42.                 <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"/>  
  43.                 <br />  
  44.                 <br />  
  45.                 <asp:Label ID="Label1" runat="server" Text="Please Select The CheckBox"></asp:Label>  
  46.                 </ContentTemplate>  
  47.         </asp:UpdatePanel>  
  48.       
  49.     </div>  
  50.     </form>  
  51. </body>  
  52. </html>  
Now the Design view looks like this.
 
 
 
Step 8: Now we will write code on the button click event to display the selected value. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace ToggleButtonExtender  
  9. {  
  10.     public partial class ToggleButton : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.   
  15.         }  
  16.   
  17.         protected void Button1_Click(object sender, EventArgs e)  
  18.         {  
  19.             Label1.Text=string.Format("I <b>{0}</b> For Terms And Conditions and I <b>{1}<b/> For Terms And Conditions",  
  20.                 (CheckBox1.Checked ? "Agree" : "Disagree"),(CheckBox2.Checked ? "Agree" : "Disagree"));  
  21.         }  
  22.     }  
  23. }  
Now we run this project and see that the output looks like this.
 
 
 
 
 
 
This is all about the AJAX ToggleButton Extender in ASP.Net.

Happy Coding. 

Up Next
    Ebook Download
    View all
    Learn
    View all