Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

Recently I have been trying to use Bootstrap Switch for my ASP.net Web Form CheckBox component. When I load the page, I can see the switch, but when I click, nothing happens. Below here is my code :

C#
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="BootStrap_Test.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <!-- Bootstrap Core CSS -->
    <link href="bootstrap/css/bootstrap.css" rel="stylesheet" />

    <!-- Custom CSS -->
    <link href="css/bootstrap-switch.css" rel="stylesheet" />

    <script src="js/jquery-1.11.1.js"></script>
    <script src="bootstrap/js/bootstrap.js"></script>
    <script src="js/bootstrap-switch.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            start_switch();
        });

        function start_switch() {
            $(".custom_check").bootstrapSwitch();
        }
    </script>

    <title></title>
</head>
<body data-spy="scroll" data-target=".navbar-fixed-top">
    <form id="form1" runat="server">
	
        <section class="container">
            <div class="col-md-12">
                <div class="row">
                    <asp:CheckBox ID="CheckBox2" CssClass="custom_check" checked runat="Server" OnCheckedChanged="CheckBox2_CheckedChanged" />
                </div>
            </div>
        </section>
		
    </form>
	
</body>
</html>
Posted
Updated 15-Oct-21 6:05am

Try this.

Change
JavaScript
$(".custom_check").bootstrapSwitch();


To
JavaScript
$(".custom_check input").bootstrapSwitch();
 
Share this answer
 
THis fixes it

ASP.NET
<div class="custom-control custom-switch">                                 
    <asp:CheckBox runat="server" ID="custom_check"/>
    <label class="custom-control-label" for='<%: custom_check.ClientID %>'></label>
</div>


ASP.NET
protected void Page_Init(object sender, EventArgs e)
{
    custom_check.InputAttributes.Add("class", "custom-control-input");
}
 
Share this answer
 
Comments
Richard Deeming 15-Oct-21 12:10pm    
This solution appears to relate to Bootstrap 4. The first alpha version of BS4 wasn't released until seven months after the OP posted this question, which means there is no way they could have been using it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900