Always One checkbox checked

Discussion in 'ASP .Net' started by RodBillett, Dec 4, 2003.

  1. RodBillett

    RodBillett Guest

    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;
    }
    }
    RodBillett, Dec 4, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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;
    > }
    > }
    >
    >
    Kevin Spencer, Dec 4, 2003
    #2
    1. Advertising

  3. RodBillett

    RodBillett Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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;
    > > }
    > > }
    > >
    > >

    >
    >
    RodBillett, Dec 4, 2003
    #3
  4. RodBillett

    RodBillett Guest

    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" <> wrote in message
    news:...
    > 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;
    > }
    > }
    >
    >
    RodBillett, Dec 4, 2003
    #4
  5. RodBillett

    bruce barker Guest

    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" <> wrote in message
    news:...
    > 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;
    > }
    > }
    >
    >
    bruce barker, Dec 4, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mehul
    Replies:
    0
    Views:
    756
    mehul
    May 3, 2004
  2. yurps
    Replies:
    1
    Views:
    6,861
    rajeshthangarasu1983
    Feb 29, 2008
  3. Eric via .NET 247

    [ASP.net / C#] Checkbox Checked Always false

    Eric via .NET 247, Mar 25, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    9,347
    dunshRB
    Mar 25, 2005
  4. VB Programmer
    Replies:
    1
    Views:
    4,399
    Ken Cox [Microsoft MVP]
    Apr 13, 2005
  5. runescience
    Replies:
    0
    Views:
    4,230
    runescience
    Feb 23, 2006
Loading...

Share This Page