After exploring various solutions, I found that the Ajax Control Toolkit produces a messy html output filled with spans and cumbersome styling.
While using css styling with ::before
tags to conceal the original control's box seemed promising, adding runat=server
to make it accessible sometimes resulted in the checkbox not registering value changes unless clicked directly on the original control.
In certain methods, if the label text was too long, it would either overlap with or end up below the checkbox on the screen.
Ultimately, following the recommendation from @dimarzionist's response on this page, I opted for an asp.net ImageButton approach. This allowed me to manage styles effectively and ascertain checkbox status via codebehind.
https://i.sstatic.net/ZUi1D.png
<asp:ImageButton ID="mycheckbox" CssClass="checkbox" runat="server" OnClick="checkbox_Click" ImageUrl="unchecked.png" />
<span class="checkboxlabel">I have read and promise to fulfill the <a href="/index.aspx">rules and obligations</a></span>
Code-behind snippet:
protected void checkbox_Click(object sender, ImageClickEventArgs e) {
if (mycheckbox.ImageUrl == "unchecked.png") {
mycheckbox.ImageUrl = "checked.png";
//Do something if user checks the box
} else {
mycheckbox.ImageUrl = "unchecked.png";
//Do something if the user unchecks the box
}
}
Furthermore, with this method, the <span>
used for the checkbox text will align perfectly with the checkbox.
https://i.sstatic.net/s60iX.png
.checkboxlabel{
vertical-align:middle;
font-weight: bold;
}
.checkbox{
height: 24px; /*height of the checkbox image*/
vertical-align: middle;
}