469,160 Members | 2,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

Always One checkbox checked

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;
}
}
Nov 18 '05 #1
4 2233
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.

"RodBillett" <No***@NoWhere.Com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
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;
}
}

Nov 18 '05 #2
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

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:en*************@TK2MSFTNGP11.phx.gbl...
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.

"RodBillett" <No***@NoWhere.Com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
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;
}
}


Nov 18 '05 #3
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;
}
"RodBillett" <No***@NoWhere.Com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
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;
}
}

Nov 18 '05 #4
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)
"RodBillett" <No***@NoWhere.Com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
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;
}
}

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by claudel | last post: by
reply views Thread by Zürcher See | last post: by
5 posts views Thread by DotNetJunkies User | last post: by
1 post views Thread by Eric via .NET 247 | last post: by
4 posts views Thread by Charlotte | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.