Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 July 15th, 2011, 01:48 PM
Authorized User
 
Join Date: Jan 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to make gridview checkbox act like radiobutton

Hi Imar,

My question is how to make checkboxes act like radiobuttons. My scenario is such that I have a GridView with a few columns where two of the columns are checkboxes. How can I make those checkboxes act like radiobuttons? I mean, if lets say checkbox1 of row 1 is checked, then if I check checkbox2 of row 1, then I'd want checkbox1 of that same row to be unchecked. I want to be able to achieve this type of validation on the client side (using javascript) though.

Thanks for the help.

Ed
 
Old July 15th, 2011, 04:02 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ed,

You're probably better off asking future questions like this in a JavaScript or jQuery forum here at p2p.wrox.com as it's a bit beyond the scope of the book, and of what I can typically support on a forum like this.

Anyway, I quickly cooked up an example as I was a bit intrigued in the solution myself.

Edit: See attachment. This forum was eating my code (due to the call to the jQuery library on the Google web site) so I moved it to an attachment.

This jQuery code first assigns some code to the click event of each checkbox. It then wraps "this" (the checkbox) in a jQuery matched set and then use parent().parent() to traverse up from the checkbox through the table cell (td) to the table row (tr).

On the row, it finds all checkboxes and disables them using attr which enables you to set an attribute value on an element. At the item, the clicked checkbox is selected.

This code only fires if the checkbox has just been turned on. Otherwise (e.g. without the if check) you wouldn't be able to unckeck both items anymore (which may be what you want though).

This code assumes the same table structure as in my sample HTML so you may need to modify it a bit to match your HTML.

Cheers,

Imar
Attached Files
File Type: txt CheckoxesThatActLikeRadios.txt (1.4 KB, 39 views)
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; July 15th, 2011 at 04:09 PM..
The Following User Says Thank You to Imar For This Useful Post:
Edkch (July 15th, 2011)
 
Old July 15th, 2011, 07:13 PM
Authorized User
 
Join Date: Jan 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to make gridview checkbox act like radiobutton

Hi Imar,

Thanks for the reply. I'll try to play with what you had and see if I can make it work with what I have. If not, I'll try to find the right forum to post this question with more details. Thanks again!!

Ed
 
Old July 21st, 2011, 01:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ed,

I wrote a quick blog about this solution with a slightly better implementation: the code now uses closest() to find the nearest table row, rather than explicitly using the parent method: http://imar.spaanjaars.com/562/creat...ns-with-jquery

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
RadioButton with GridView Manoj Bisht ASP.NET 3.5 Professionals 1 March 18th, 2009 12:13 PM
Checkbox in Gridview yukijocelyn ASP.NET 2.0 Basics 0 September 24th, 2007 12:32 AM
Checkbox in GridView aliirfan84 ASP.NET 2.0 Professional 1 May 15th, 2007 03:59 PM
Dynamic checkbox and radiobutton lists Ric_H C# 1 April 5th, 2006 05:04 AM
Dynamic checkbox and radiobutton lists Ric_H General .NET 2 March 30th, 2006 01:52 PM





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