Change Style/Look of Asp:CheckBox using CSS - asp.net

I want to change the standard "3D" look of the standard asp.net checkbox to say solid 1px. If I try to apply the styling to the Border for example it does just that - draws the standard checkbox with a border around it - which is valid I guess.
Anyway, is there a way to change how the actual textbox is styled?

Rather than use some non-standard control, what you should be doing is using un-obtrusive javascript to do it after the fact. See http://code.google.com/p/jquery-checkbox/ for an example.
Using the standard ASP checkbox simplifies writing the code. You don't have to write your own user control, and all your existing code/pages don't have to be updated.
More importantly, it is a standard HTML control that all browsers can recognize. It is accessible to all users, and work if they don't have javascript. For example, screen readers for the blind will be able to understand it as a checkbox control, and not just an image with a link.

I think the best way to make CheckBox looks really different is not to use checkbox control at all. Better use your own images for checked/unchecked state on-top of hyperlink or image element. Cheers.

None of the above work well when using ASP.NET Web Forms and Bootstrap.
I ended up using Paul Sheriff's Simple Bootstrap CheckBox for Web Forms
<style>
.checkbox .btn, .checkbox-inline .btn {
padding-left: 2em;
min-width: 8em;
}
.checkbox label, .checkbox-inline label {
text-align: left;
padding-left: 0.5em;
}
.checkbox input[type="checkbox"]{
float:none;
}
</style>
<div class="form-group">
<div class="checkbox">
<label class="btn btn-default">
<asp:CheckBox ID="chk1" runat="server" Text="Required" />
</label>
</div>
</div>
The result looks like this...

Simplest best way, using the ASP checkbox control with custom design.
chkOrder.InputAttributes["class"] = "fancyCssClass";
you can use something like that.. hope that helps

paste this code in your css and it will let you customize your checkbox style. however, it's not the best solution, it's pretty much displaying your style on top of the existing checkbox/radiobutton.
input[type='checkbox']:after
{
width: 9px;
height: 9px;
border-radius: 9px;
top: -2px;
left: -1px;
position: relative;
background-color: #3B8054;
content: '';
display: inline-block;
visibility: visible;
border: 3px solid #3B8054;
transition: 0.5s ease;
cursor: pointer;
}
input[type='checkbox']:checked:after
{
background-color: #9DFF00;
}

Why not use Asp.net CheckBox button with ToggleButtonExtender available from the Ajax control toolkit.

Not sure that it's really an asp.net related question.. Give this a shot, lots of good info here:
http://www.456bereastreet.com/archive/200409/styling_form_controls/

Keep in mind that the asp:CheckBox control actually outputs more than just a single checkbox input.
For example, my code outputs
<span class="CheckBoxStyle">
<input id="ctl00_cphContent_cbCheckBox"
name="ctl00$cphContent$cbCheckBox"
type="checkbox">
</span>
where CheckBoxStyle is the value of the CssClass attribute applied to the control and cbCheckBox is the ID of the control.
To style the input, you need to write CSS to target
span.CheckBox input {
/* Styles here */
}

They're dependent on the browser really.
Maybe you could do something similar to the answer in this question about changing the file browse button.

Well, I went through every solution I could find.
The Ajax Control Toolkit works, but it creates a weird html output with all kinds of spans and other styling that is hard to work with.
Using css styling with the ::before tags to hide the original control's box would work, but if you placed runat=server into the element to make it accessible to the code-behind, the checkbox would not change values unless you actually clicked in the original control.
In some of the methods, the entire line for the label would end up under the checkbox if the text was too long for the viewing screen, or would end up underneath the checkbox.
In the end, (on the adice of #dimarzionist's answer here in this page) I used an asp.net ImageButton and used the codebehind to change the image. With this solution I get nice control over the styles and can determine whether the box is checked from the codebehind.
<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 rules and obligations</span>
And in the code-behind
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
}
}
What's more, is with this method, The <span> you use for the checkbox's text will wrap perfectly with the checkbox.
.checkboxlabel{
vertical-align:middle;
font-weight: bold;
}
.checkbox{
height: 24px; /*height of the checkbox image*/
vertical-align: middle;
}

Related

How to make checkbox tri-state?

Technology Used : Angular 5
Scenario :
I have a tree view with a checkbox. Whenever data is loaded onto the page, based on the property I am making checkbox checked or unchecked.
Todo :
Whenever property is false I want to add a cross mark in checkbox by default.
Below is my code which will render tree view with the checkbox.
<tree-root #tree [options]="options" [nodes]="nodes">
<ng-template #treeNodeTemplate let-node="node" let-index="index">
<input (change)="check(node, !node.data.checked)"
type="checkbox"
[indeterminate]="node.data.indeterminate"
[checked]="node.data.checked" class="css-checkbox">
{{ node.data.name }}
</ng-template>
</tree-root>
In the above code snippet whenever node.data.checked is true then I am making checkbox checked by default. Whenever node.data.checked comes false I want to display cross mark in the checkbox.
I have searched over the net but did not get any use full links.
Can someone help me to make this work?
Any help would be greatly appreciated. Thank you.
When you trigger [checked], the :checked pseudo-class will be applied. You can use this to target checkboxes which have been checked (either manually or programmatically).
Adding a cross in the checkbox is a little harder, as you cannot add one to an input[type=checkbox] itself. To get around this, you can hide the checkbox itself (with display: none), and make use of a <label> which is associated with the checkbox, bound by the label's for attribute. By using for, when clicking on the label you'll simulate clicking on the checkbox itself.
The label can be styled to look like the original checkbox, and also offers extra flexibility, allowing for things like a background-image (which can be used to show the cross).
Assuming your <label> elements come directly after your <input> elements, you can target them with the adjacent sibling combinator (+), with selectors such as input + label. This can be chained with the :checked pseudo-class to style the labels for the checkboxes that are checked: input:checked + label.
Here's an example:
input[type=checkbox].css-checkbox {
display: none;
}
input[type=checkbox].css-checkbox + label.css-label {
padding-left: 20px;
height: 15px;
display: inline-block;
line-height: 15px;
background-repeat: no-repeat;
background-position: 0 0;
font-size: 15px;
vertical-align: middle;
cursor: pointer;
}
input[type=checkbox].css-checkbox:checked + label.css-label {
background-position: 0 -15px;
}
.css-label {
background-image: url(http://csscheckbox.com/checkboxes/lite-x-red.png);
}
<input id="one" class="css-checkbox" type="checkbox" />
<label for="one" class="css-label"></label>
<input id="two" class="css-checkbox" type="checkbox" />
<label for="two" class="css-label"></label>
While a checkbox can only ever have two states (either checked or unchecked), what is possible with Angular is to update the associated class of the element. If you want a tick (or similar) as well, this is your best bet.
Simply create one class for when the value is truthy, one for when it is falsy, and apply the relevant class to the label depending on the value. The positive class would have a check background-image, and the negative class would have a cross. The 'default' would show up empty, as is in the above example.

Change look of TextBox control

Is there a way I can change the appearance of a TextBox from its default look to look like this
I searched for creating a custom TextBox but didnt see anything about changing how it looked.
I have the have the image in a PSD i just didnt know if there was a way to replace the default look with this image
I am new to making websites and just using this for learning purposes so I dont really know where to start
You should use CSS to achieve this. What you need to do is to style it. Here are some nice examples that could help you.
Here is the one that shows how to add background image:
.tb11 {
background:#FFFFFF url(images/search.png) no-repeat 4px 4px;
padding:4px 4px 4px 22px;
border:1px solid #CCCCCC;
width:230px;
height:18px;
}
You can create a custom asp.net text box control and wrap oob asp.net text box with necessary html (a label or span) and css to style it like the way you want. That control will become reusable and you would be able to use it on any of your pages.
If you want an easy way to do it, just load easy to use bootstrap framework and include the needed file to your project.
Than just add the right class to your control and here it is !
Also, the docs is really complete and simple
http://getbootstrap.com/components/#input-groups
An asp.net TextBox is really just an input with the type="text".
You can do something like this using stylesheets:
CSS:
input[type=text].styled1
{
background: url(http://myUrl.com/Username.jpg);
border:0;
color: gray;
height: 23px;
padding-left:10px;
width: 200px;
}
XHTML:
<asp:Textbox id="txtUsername" runat="server" CssClass="styled1" />

Mouse hover event not working for the second time

I am using asp.net text box and I am showing the border for the text box on mouse hover event and its pretty well doing it's job for the first time and when I click on the text box and again click outside the textbox and If I mouse hover the same textbox it dosen;t show me the border.
Can anyone say me where I am doing wrong?
Thanks in advance!
Here is my css code:
.onfocus
{
border-width: thin;
border-color: #0033CC;
}
.onblur
{
border:0px;
}
.MyTextbox:hover{border:1px solid red;}
Here is how I am using it:
<script type ="text/javascript">
function Change(obj, evt) {
if (evt.type == "focus")
obj.className = "onfocus";
else if (evt.type == "blur")
obj.className = "onblur";
}
<asp:TextBox ID="txtUsername" runat="server" Style="position: absolute; top: 76px;
left: 24px; width: 189px; height: 24px; outline: none;" onfocus ="Change(this, event)"
onblur ="Change(this, event)" BackColor="#FAFFBD" CssClass="MyUsername" ></asp:TextBox>
" outline: none;"
where is the style attr ?
The hover is not working the second time because your javascript code on focus/blur is changing the class value for the textbox so that it no longer has the "MyTextbox" class. Since your border change on hovering is done based on the MyTextbox class, it no longer works after that.
What you should be doing instead of setting obj.className = "onfocus", is to add the "onfocus" class to the existing value so that it is not lost. Then, during the blur event, you would remove the onfocus class, and add in the onblur class (again, not just totally replacing the className value).
This question has some good answers about how you can properly add/remove the extra classes in either plain javascript or with jQuery (which makes it much easier).

Multi-color text in ASP.NET Button

Wondering if there is a way to have multicolor text for ASP.NET Button Text.
Or what is the best way to have multi-styled text e.g. bold, red-color + normal black color text for ASP.NET button?
One way I know is creating an Image and use ImageButton, which I plan to do if I don't find any other better way.
Any other ways?
Updated:
Why need??
1: The button has little informative message and part of the text needs to be differnt for emphasizing.
2: Not my choice.
One way I figured out: is using a LinkButton. I'll post my solution once ready.
I believe the XHTML schema allows you to do:
<button><span>Hello</span> <span>World</span></button>
Which you can style accordingly with CSS.
.button span { color: red; }
.button span:first-child { color: blue; }
For an ASP.NET button, you could probably write the someting similar, but realistically it is not a great UX recommendation. If you can keep to a consistent UI, or try to follow the UI guidelines outlined by the parent OS, the user will be more familiar and comfortable.
Here's my solution:
.btndiv
{
border-style: outset;
background-color: Lime;
color: Black;
width: 200px;
height: 50px;
text-align: center;
}
.btndiv a
{
text-decoration: none;
color: black;
}
<p class="btndiv">
<asp:Linkbutton id="LinkButton1" runat="server">
<span style="color:Red;font-weight:bold">Welcome Back! </span><br />
<span> Click to Enter the site.</span>
</asp:Linkbutton>
</p>
Have you considered using an <a> tag instead of a button,
and then calling any function on the click event of the tag?
Particletree (written by the guys behind Wufoo forms) has an
informative article about styling buttons.
http://particletree.com/features/rediscovering-the-button-element/
Are you sure you want button text with multiple styles?
Do you have examples of a well-designed button that has
multiple text styles? (I ask because I don't recall ever seeing one...)
For reference:
Apple Mac UX guidelines and buttons:
http://developer.apple.com/library/mac/#documentation/userexperience/conceptual/applehiguidelines/XHIGControls/XHIGControls.html#//apple_ref/doc/uid/TP30000359-TPXREF186
Windows Vista UX guidelines and command buttons:
http://msdn.microsoft.com/en-us/library/aa511453.aspx

CSS class not being applied to input element on asynchronous call back

I am using the following CSS class to hide a textbox in an asp:UpdatePanel to accept input from a USB card reader.
<style type="text/css">
.USBBox
{
position: absolute;
left: -999em;
}
</style>
When I click an asp:LinkButton control that is configured to be an asp:AsyncPostBackTrigger for the update panel the control appears on the page and the CSS class is not applied to the asp:TextBox control.
This behavior is displayed in IE7. It works as expected in FireFox 3.5.7
What would cause this behavior and how do I resolve it
There my be a specificity issue. Try
input.USBBox{
position:absolute!important;
left:-999px!important;
}
And if it works, back out of the !important tags to see what actually caused the issue.
Also declare display:block; just in case.
I think you should use:
.USBBox
{
display: none;
}
or maybe use a asp:HiddenField instead of a textbox.
try
.USBBox
{
display: block;
width: 100px; /* or however wide you want it */
position: absolute;
left: -999em;
background: #ff0000; /* visually ensure the class style is being applied, remove it later */
}
position should only work on items that are displayed as a block. form items by default are displayed inline.
also just for giggles set the background color just to make sure the input box is taking class.
Could it be that the new control comes with multiple classes ?
Because IE is having issues when combining classes on a single element..

Resources