CheckBox selection

G

Guest

Hi,

I have a 5 checkboxes and I would like to be able to use them as grouped
radiobuttons or a checkboxlist (one selection at the time) in the client side
without having to use a autopostback.
I think the only way is javascript but I don't know how. does any one have a
sample code to do that?


Cheers
 
L

Laurent Bugnion [MVP]

Hi,
Hi,

I have a 5 checkboxes and I would like to be able to use them as grouped
radiobuttons or a checkboxlist (one selection at the time) in the client side
without having to use a autopostback.
I think the only way is javascript but I don't know how. does any one have a
sample code to do that?


Cheers

I would recommend against that, because checkboxes are not destined to
behave like radiobuttons, it's confusing the user and is bad ergonomy.

That said, you can programatically check or uncheck a checkbox in
JavaScript using this code:

// The checkbox's ID is myCheckbox1
var cb = document.getElementById( "myCheckbox1" );

if ( cb != null
&& cb.checked != null )
{
cb.checked = true; // or false
}

HTH,
Laurent
 
G

Guest

Yes, but I could find any OnClient event for checkboxes to call this
javascript code and I tried to run it in "OnCheckedChanged" but it looks that
this event needs the autopostback turned on!!!?
sorry, i'm a beginner in asp but how can I run this code to be executed in
client side

Laurent Bugnion said:
Hi,
Hi,

I have a 5 checkboxes and I would like to be able to use them as grouped
radiobuttons or a checkboxlist (one selection at the time) in the client side
without having to use a autopostback.
I think the only way is javascript but I don't know how. does any one have a
sample code to do that?


Cheers

I would recommend against that, because checkboxes are not destined to
behave like radiobuttons, it's confusing the user and is bad ergonomy.

That said, you can programatically check or uncheck a checkbox in
JavaScript using this code:

// The checkbox's ID is myCheckbox1
var cb = document.getElementById( "myCheckbox1" );

if ( cb != null
&& cb.checked != null )
{
cb.checked = true; // or false
}

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion [MVP]

Hi,
Yes, but I could find any OnClient event for checkboxes to call this
javascript code and I tried to run it in "OnCheckedChanged" but it looks that
this event needs the autopostback turned on!!!?
sorry, i'm a beginner in asp but how can I run this code to be executed in
client side

First you need to understand what is running on the client (JavaScript)
and what runs on the server (C#, VB.NET). The events you set in C# code
will be executed on the server, so of course it needs a postback.

Events exist in JavaScript too, however the syntax is different.

For example:

<input type="checkbox" onclick="doSomething();" />

HTH,
Laurent
Laurent Bugnion said:
Hi,
Hi,

I have a 5 checkboxes and I would like to be able to use them as grouped
radiobuttons or a checkboxlist (one selection at the time) in the client side
without having to use a autopostback.
I think the only way is javascript but I don't know how. does any one have a
sample code to do that?


Cheers
I would recommend against that, because checkboxes are not destined to
behave like radiobuttons, it's confusing the user and is bad ergonomy.

That said, you can programatically check or uncheck a checkbox in
JavaScript using this code:

// The checkbox's ID is myCheckbox1
var cb = document.getElementById( "myCheckbox1" );

if ( cb != null
&& cb.checked != null )
{
cb.checked = true; // or false
}

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top