0

I have page where switch toggle is used and when toggle is switched to on and off update value in database.

<div class="card rounded" style="margin-bottom: 20px">
            <asp:Image ID="coverpic" runat="server" CssClass="card-img" Height="500" AlternateText="Cover Picture" />
            <div class="card-img-overlay card-inverse social-profile d-flex " style="top: auto">
                <div class="align-self-center rounded" style="margin: auto">
                    <asp:Image ID="profilepic" runat="server" CssClass="img-circle" Height="100" Width="100" AlternateText="Profile Picture" />
                    <h4 class="card-title">
                        <asp:Label ID="fullname" runat="server"></asp:Label></h4>
                    <h6 class="card-subtitle">
                        <asp:Label ID="unametop" Font-Size="Large" runat="server"></asp:Label></h6>
                    <div class="switch">
                        <label>
                            User
                            <input id="userrole" type="checkbox" runat="server"><span class="lever switch-col-red"></span>Admin</label>
                    </div>
                </div>
            </div>
        </div>

As you can see input checkbox with switch class is used, when i turn switch on i want to update user role in database to admin and when switch is turned off i want to update user role to normal user. Please help if you can.

Max
  • 1
  • 3

1 Answers1

0

By design the input control doesn't have a click event, so in my opinion the most elegant way to do it is with jQuery, if you're using it. See this answer.

Alternatively you could do something like this:

<script>
  function triggerpostback(obj) {
    //check if textbox is readyonly
    if (obj.readOnly) {
      __doPostBack('Button', "");
    }
  }

</script>
<asp:Button ID="Button" runat="server" Text="Button" OnClick="Button_Click" style="display:none" />
<input id="userrole" type="checkbox" runat="Server" onclick="triggerpostback(this)" />

Code-behind:

protected void Button_Click(object sender, EventArgs e)
{
    // Add the code to update user role in database
}
iamdlm
  • 1,885
  • 1
  • 11
  • 21