Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
How To Create Read Only CheckBox In ASP.NET
CheckBox control is standard ASP.NET control, used to take user's input when user should choose between two opposite options like yes/no, checked/unchecked, true/false etc.
Sometimes you need just to show CheckBox current value, and display control as checked or unchecked. For example, if CheckBox is on some report which should be printed, CheckBox's state should be read-only, without option to change it on report just before printing. In this case, if user can change report values and then print report, printed document would contain wrong information.
TextBox control has very useful ReadOnly property. If ReadOnly is set to true, user can't change value of TextBox control. Unfortunately, there is no similar ReadOnly property for CheckBox control.
CheckBox control has Enabled property which could be used to make CheckBox read only. Use Enabled="false" and user can't change CheckBox value. The problem is, when Enabled is false, CheckBox will change its look and become gray. If you background is also gray, disabled CheckBox control almost disappear on form.
Reset CheckBox value by using AutoPostBack = true and CheckedChanged server side event
First way to make CheckBox read-only is to return control's original state back when user tries to change CheckBox value. On this way, after postback, CheckBox control will still have unchanged value. To make this work, set AutoPostBack property to true, and in CheckedChanged event, re-set CheckBox.Checked property to previous state.
Advantage of this method is that you are able to not just keep value of CheckBox unchanged, but also you can inform the user that this form is read only and values in report shouldn't be changed. However, it requires postback to web server, which is not user friendly in every case.
<asp:CheckBox runat="server" ID="chkReadOnly" Checked="true" OnClick="return false;" />
Using jQuery to make CheckBox ReadOnly
Here is an example which makes read-only all check boxes on page.
Use two images instead of CheckBox control
Instead of using CheckBox control, you can use two images: Checked.gif or Unchecked.gif, and depending of current value show one of them. These images could look like CheckBox control, so your report will look like there is real CheckBox.
If Checked property is true, show Checked.gif, and if Checked = false, show Unchecked.gif image. And finally you have read-only "checkbox".
Be aware that using of screen shots of checked and unchecked CheckBox could confuse visitors if they try to click on it. As an alternative, you can use any two images that represent checked and unchecked state. On this way you have an opportunity to enhance user interface, design beautiful check symbols and make pages more intuitive.
In the other hand, simple Checked and Unchecked images are always displayed perfectly.
Displaying of images instead of CheckBox control requires additional code. If we need read-only CheckBox on more than one place, we should consider encapsulation of logic inside of custom ASP.NET control.
Enhanced standard CheckBox control as ReadOnlyCheckBox custom control
To automate things, we will encapsulate "use images instead of CheckBox control" idea in form of custom ASP.NET control. Let's name it ReadOnlyCheckBox control. ReadOnlyCheckBox will inherit standard CheckBox control and have all its features, and few new properties for its read-only behavior.
I created ReadOnlyCheckBox control which inherits standard ASP.NET CheckBox control. It has all the same properties, and only render itself as read-only at run time. You can use Checked/Unchecked images, or display messages like Yes/No, True/False etc. ReadOnlyCheckBox control is free
You can download ReadOnlyCheckBox control and example project from https://beansoftware.com/download/ReadOnlyCheckBox.zip.
HTML CheckBox is created by using <input > tag, so its basic purpose is to gather input from the user. CheckBox has not ReadOnly property, and by default can't be used to just show data. CheckBox is created to be interactive. That opens a question that maybe idea to try to use input control for data presentation is wrong in its essence?
Although it is possible to make CheckBox control read only, I think we could find better answer if we look into relation between ASP.NET TextBox and Label controls.
CheckBox control is made to work with bool variables, like Yes/No, True/False, Checked/Unchecked etc. In the other hand, TextBox and Label controls both work with string data type. TextBox control is designed to accept string input from user, and on the client side is rendered as input tag just like CheckBox. Label control is designed as read-only.
By following the same logic, we need some additional ReadOnlyCheckBox control, which would only display checked/unchecked states. Unfortunately, control like this doesn't exist on Visual Studio Toolbox. But, you can create it! Even better, you can use mine :)
You can download ReadOnlyCheckBox control project and one example project from https://beansoftware.com/download/ReadOnlyCheckBox.zip. The code is free and it looks without bugs for me. But, please note that you use it on your own responsibility.