Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 5th, 2007, 11:29 PM
Authorized User
Join Date: Feb 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default checkbox in gridview and setting "checked"


I have read many posts about the gridview and checkbox but I have not found an answer to my question.

I am attempting to use a checkbox for a binded column of data that says either yes or no. Unfortunately it is not boolean. Is there a way to set the "checked" status of each row in code based upon the binded data.

I thought I could write an "If then" to capture yes and set the checkbox to yes, but I can't access the Item Template from the code behind file.

Any help would be appreciated.

Old April 7th, 2007, 03:46 AM
Friend of Wrox
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post

Well instead of CheckBoxField, why not use TemplateField, find the checkbox id in DataBound event, and perform operation accordingly.

     <input type=checkbox id=chk runat=server />


Fortune favours the brave, so don't regret on missed oppurtunities.
Old April 7th, 2007, 05:15 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Alternatively, you can have a GetBoolean method in the code behind that returns a Boolean based on the value, like this:

Public Function GetBoolean(ByVal theValue As Object) As Boolean
  Return theValue.ToString().ToLower() = "true"
End Function

This value returns True when you pass in the word True, and False otherwise. You can call it like this:

<asp:CheckBox .... Checked='<%# GetBoolean(Eval("YourColumnName"))%>' .. />

Hope this helps,


Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkbox checked all mateenmohd Javascript 1 January 2nd, 2007 06:19 AM
checkbox checked by default by html:checkbox sachin.tathod Struts 3 December 4th, 2006 03:41 PM
Checkbox checked? ninel ASP.NET 2.0 Professional 1 April 12th, 2006 10:50 PM
allow only one checkbox to be checked! morpheus HTML Code Clinic 5 April 15th, 2004 10:59 AM
Setting Checkbox Checked Property from OnClick saturdave ASP.NET 1.0 and 1.1 Basics 2 April 8th, 2004 09:33 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.