Always One checkbox checked

  • Thread starter Thread starter RodBillett
  • Start date Start date
R

RodBillett

I have a situation where I have 3 checkboxes - and at least 1 needs to be
selected at all times...

I have implemented the code that allows this behavior to happen, BUT
Viewstate gets all messed up. As long as none of the checkboxes are
disabled, viewstate works fine - and client actions are 'remembered' after a
postback. but when I have one checkbox and it is disabled, then when I
postback, ALL checkboxes get the unchecked value.

Thanks
Rod

on the server, when I create the checkboxes, i add the following attribute
to all. and they are defaulted to all 'Checked=true"

SearchWhat_Annotations.Attributes.Add( "onclick", "AlwaysOneChecked(
this.form )" );

<Client Script>
function AlwaysOneChecked( thisForm )
{
var CheckedCount = CheckedCount = (thisForm.SearchContents.checked?1:0) +
(thisForm.SearchProfiles.checked?1:0) +
(thisForm.SearchAnnotations.checked?1:0);

if ( CheckedCount == 1 )
{
thisForm.SearchContents.readonly =
(thisForm.SearchContents.checked?true:false)
thisForm.SearchProfiles.disabled =
(thisForm.SearchProfiles.checked?true:false)
thisForm.SearchAnnotations.disabled =
(thisForm.SearchAnnotations.checked?true:false)
}
else
{
thisForm.SearchContents.disabled = false;
thisForm.SearchProfiles.disabled = false;
thisForm.SearchAnnotations.disabled = false;
}
}
 
Changes made to the state of the objects on the client via JavaScript do not
affect ViewState, which is what ASP.Net uses to keep track of the state of
the Controls on the client. In other words, when you make a change via
JavaScript, it is not recorded in ViewState. When the next PostBack occurs,
the ViewStyate is used on the server to put the objects back into the state
they were in according to ViewState.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Well, that is only partially true.

Lets say that a page rendered to the client has Checkbox 1 and 2 are Checked
(So in viewstate, they are both remembered as 'checked'). Then I click
Checkbox2 to uncheck it. The JScript then disables checkbox1 - but it still
appears checked. I now force another postback! Both on the server during
processing and in the re-rendered page at the client, it has a value of
unchecked for checkbox1!

So it didnt really use the viewstate it had - It appears more like since the
control is disabled, all the server side framework processing 'forgets about
the poor little control!'

Rod
 
OK - I can get partway to where i want to by modifying the script to cancel
the click event (as listed below). But that does not render the checkbox in
'disabled' mode. Apparently, this appears very confusing to non technical
people - LIKE USERS - therefore, I still need to render the checkbox -
disabled!! (Anyone else have the problem with marketing people wanting
windows aplications on the Web?)

Thanks in Advance!
Rod

function AlwaysOneChecked( thisForm )
{
var CheckedCount = CheckedCount = (thisForm.SearchContents.checked?1:0) +
(thisForm.SearchProfiles.checked?1:0) +
(thisForm.SearchAnnotations.checked?1:0);

if ( CheckedCount == 0 )
window.event.returnValue = false;
else
window.event.returnValue = true;
}
 
the browser will not postback a disabled or unchecked checkbox, so to the
server it looks exactly the same. if you want to disable a checked checkbox,
then you must enable it before submit, or store the value in a hidden field.

-- bruce (sqlwork.com)
 
Back
Top