Checkbox does not respect checked value if disabled client side

S

Stu Carter

Hi,

ENV: Windows 2003 Server, IE 6, .Net 1.1 SP1, VS 2003

We have an ASP.Net server side Checkbox on a web page. The checkbox is
disabled by some client-side JavaScript due to user interaction with some
other controls on the page (i.e. the checkbox option is no longer
applicable). We do this with JavaScript because a postback looks poor and
takes time.

This works fine. On Page_Unload, we want to store the last value of the
checkbox in Session state, so we can reset the state when the user returns
to this page later.

However, on a postback, the properties of the disabled checkbox when it is
checked are:
Enabled = true
Checked = false

So, the Enabled state is wrong/hasn't been changed, but the checked state
has been changed and is wrong! The control properties should be:
Enabled = false
Checked = true

Can you please let me know why this is, and is there a fix?

I know that using an HTML checkbox and running it server side is an option,
but you then can't have a label alongside the checkbox that triggers
checking/unchecking the checkbox...sigh.

Thanks,
Stuart
 
S

Steven Cheng[MSFT]

Hi Stu,

Welcome to ASPNET newsgroup.
From your description, you're using the ASP.NET CheckBox server control on
your webpage and due to some reason, you use clientside script to control
the checkbox's enable/disable at clientside(through the <input
type=checkbox..> 's "disabled" attribute ,yes?) . However , you found that
when the checkbox is disabled at clientside, when postback, the checkbox's
"Enabled" and "Checked" property's value seems to be different from your
expected value, yes?

As for this problem, the behavior you've met is the expected one due to the
ASP.NET server control's control model and state persistent mechanism. For
asp.net webserver control, the Enabled and other propeties like "Checked"
for checkbox are all serverside properties which is active only during the
asp.nte page(or control)'s serverside lifecycle, after the page has been
output to client(page lifecycle end), these properties be default are
persisted into page's ViewState(if enabled), and then, the ViewState won't
be modified at clientside (that's why clientside script's modification on
clientside html element won't affect the control's serverside propertis
value). So this explain why the "Enabled" property is not as you expected
when you modify the "disabled" attribute for the clientside element.

AS for the "Checked" property, it is also related to another fact, the
ASP.NET checkbox control's "Checked" property is populated through the
following steps when page postback,

1. it will check the client post message's form collection to see whethe
the clientside <input type=checkbox...> element's value is posted, if so,
it is marked as the new value, also, the old value from the ViewState is
also retrieved, and if they're different, the new value will replace the
old value. However, when you disabled the checkbox at clientside, the
value won't be posted to server in the http message (this is the same for
other html form elemetn such as <input type=text ...> ...).
That's why when you disable the checkbox at clientside, you won't get the
updated checkbox's state at serverside.

In addition, here are some msdn reference on asp.net servercontrol's
lifecycle model and ViewState:

#Control Execution Lifecycle
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
fecycle.asp?frame=true

#Understanding ASP.NET View State
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/viewstate.asp

Hope helps. If you have anything unclear, please feel free to post here.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)






--------------------
| From: "Stu Carter" <[email protected]>
| Subject: Checkbox does not respect checked value if disabled client side
| Date: Thu, 29 Sep 2005 13:58:08 +0100
| Lines: 34
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: brooklyn-bridge.emea.veritas.com 62.172.234.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:11072
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| ENV: Windows 2003 Server, IE 6, .Net 1.1 SP1, VS 2003
|
| We have an ASP.Net server side Checkbox on a web page. The checkbox is
| disabled by some client-side JavaScript due to user interaction with some
| other controls on the page (i.e. the checkbox option is no longer
| applicable). We do this with JavaScript because a postback looks poor
and
| takes time.
|
| This works fine. On Page_Unload, we want to store the last value of the
| checkbox in Session state, so we can reset the state when the user
returns
| to this page later.
|
| However, on a postback, the properties of the disabled checkbox when it
is
| checked are:
| Enabled = true
| Checked = false
|
| So, the Enabled state is wrong/hasn't been changed, but the checked state
| has been changed and is wrong! The control properties should be:
| Enabled = false
| Checked = true
|
| Can you please let me know why this is, and is there a fix?
|
| I know that using an HTML checkbox and running it server side is an
option,
| but you then can't have a label alongside the checkbox that triggers
| checking/unchecking the checkbox...sigh.
|
| Thanks,
| Stuart
|
|
|
 
S

Stu Carter

Hi Steven,

Thanks very much, I understand now.

So since any control's state will not be posted back if it has been disabled
via client side script, the two options are:

1) Disable the checkbox in a postback.
But that means a lot of postbacks when users are configuring other options
which isn't nice.

2) Add a client-side 'OnClicked' handler for the checkbox and always record
it's current state in a hidden TextBox that runs on the server (it has to be
hidden via client-side script and enabled!). Then in Page_Unload, retrieve
the true state of the CheckBox from the TextBox :)

Give me a windows app any day!

Cheers,
Stu
 
S

Steven Cheng[MSFT]

Hi Stu,

Thanks for your response.

Yes, you're right. Also, I think your #2 option is a good idea, using
hidden field is my favorite , too :).
In addition, if you think it' ok that we makes the checkbox hidden instead
of disabling it, we can try using clientside script to hide the checkbox
when we used to disable it. How do you think?

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Stu Carter" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Checkbox does not respect checked value if disabled client
side
| Date: Fri, 30 Sep 2005 11:27:46 +0100
| Lines: 152
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: brooklyn-bridge.emea.veritas.com 62.172.234.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:11093
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi Steven,
|
| Thanks very much, I understand now.
|
| So since any control's state will not be posted back if it has been
disabled
| via client side script, the two options are:
|
| 1) Disable the checkbox in a postback.
| But that means a lot of postbacks when users are configuring other
options
| which isn't nice.
|
| 2) Add a client-side 'OnClicked' handler for the checkbox and always
record
| it's current state in a hidden TextBox that runs on the server (it has to
be
| hidden via client-side script and enabled!). Then in Page_Unload,
retrieve
| the true state of the CheckBox from the TextBox :)
|
| Give me a windows app any day!
|
| Cheers,
| Stu
|
|
| | > Hi Stu,
| >
| > Welcome to ASPNET newsgroup.
| > From your description, you're using the ASP.NET CheckBox server control
on
| > your webpage and due to some reason, you use clientside script to
control
| > the checkbox's enable/disable at clientside(through the <input
| > type=checkbox..> 's "disabled" attribute ,yes?) . However , you found
that
| > when the checkbox is disabled at clientside, when postback, the
checkbox's
| > "Enabled" and "Checked" property's value seems to be different from your
| > expected value, yes?
| >
| > As for this problem, the behavior you've met is the expected one due to
| > the
| > ASP.NET server control's control model and state persistent mechanism.
| > For
| > asp.net webserver control, the Enabled and other propeties like
"Checked"
| > for checkbox are all serverside properties which is active only during
the
| > asp.nte page(or control)'s serverside lifecycle, after the page has been
| > output to client(page lifecycle end), these properties be default are
| > persisted into page's ViewState(if enabled), and then, the ViewState
won't
| > be modified at clientside (that's why clientside script's modification
on
| > clientside html element won't affect the control's serverside propertis
| > value). So this explain why the "Enabled" property is not as you
expected
| > when you modify the "disabled" attribute for the clientside element.
| >
| > AS for the "Checked" property, it is also related to another fact, the
| > ASP.NET checkbox control's "Checked" property is populated through the
| > following steps when page postback,
| >
| > 1. it will check the client post message's form collection to see whethe
| > the clientside <input type=checkbox...> element's value is posted, if
so,
| > it is marked as the new value, also, the old value from the ViewState is
| > also retrieved, and if they're different, the new value will replace the
| > old value. However, when you disabled the checkbox at clientside, the
| > value won't be posted to server in the http message (this is the same
for
| > other html form elemetn such as <input type=text ...> ...).
| > That's why when you disable the checkbox at clientside, you won't get
the
| > updated checkbox's state at serverside.
| >
| > In addition, here are some msdn reference on asp.net servercontrol's
| > lifecycle model and ViewState:
| >
| > #Control Execution Lifecycle
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
| > fecycle.asp?frame=true
| >
| > #Understanding ASP.NET View State
| >
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
| > /viewstate.asp
| >
| > Hope helps. If you have anything unclear, please feel free to post here.
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| >
| > --------------------
| > | From: "Stu Carter" <[email protected]>
| > | Subject: Checkbox does not respect checked value if disabled client
side
| > | Date: Thu, 29 Sep 2005 13:58:08 +0100
| > | Lines: 34
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: brooklyn-bridge.emea.veritas.com 62.172.234.2
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:11072
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi,
| > |
| > | ENV: Windows 2003 Server, IE 6, .Net 1.1 SP1, VS 2003
| > |
| > | We have an ASP.Net server side Checkbox on a web page. The checkbox
is
| > | disabled by some client-side JavaScript due to user interaction with
| > some
| > | other controls on the page (i.e. the checkbox option is no longer
| > | applicable). We do this with JavaScript because a postback looks poor
| > and
| > | takes time.
| > |
| > | This works fine. On Page_Unload, we want to store the last value of
the
| > | checkbox in Session state, so we can reset the state when the user
| > returns
| > | to this page later.
| > |
| > | However, on a postback, the properties of the disabled checkbox when
it
| > is
| > | checked are:
| > | Enabled = true
| > | Checked = false
| > |
| > | So, the Enabled state is wrong/hasn't been changed, but the checked
| > state
| > | has been changed and is wrong! The control properties should be:
| > | Enabled = false
| > | Checked = true
| > |
| > | Can you please let me know why this is, and is there a fix?
| > |
| > | I know that using an HTML checkbox and running it server side is an
| > option,
| > | but you then can't have a label alongside the checkbox that triggers
| > | checking/unchecking the checkbox...sigh.
| > |
| > | Thanks,
| > | Stuart
| > |
| > |
| > |
| >
|
|
|
 
S

Stu Carter

In addition, if you think it' ok that we makes the checkbox hidden instead
of disabling it, we can try using clientside script to hide the checkbox
when we used to disable it. How do you think?

I just meant to hide the TextBox, not the Checkbox! In most circumstances,
I don't think dynamically hiding UI elements is very nice (as opposed to
just disabling them), but it's certainly another option!

Ta
 
S

Steven Cheng[MSFT]

Yes, hidden textbox is somewhat the same as the <input type="hidden"...>
element. Anyway, what a pity that the checkbox dosn't have the readonly
attribute as textbox.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Stu Carter" <[email protected]>
| References: <[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: Checkbox does not respect checked value if disabled client
side
| Date: Mon, 3 Oct 2005 15:18:15 +0100
| Lines: 243
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: brooklyn-bridge.emea.veritas.com 62.172.234.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:11131
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| > In addition, if you think it' ok that we makes the checkbox hidden
instead
| > of disabling it, we can try using clientside script to hide the checkbox
| > when we used to disable it. How do you think?
|
| I just meant to hide the TextBox, not the Checkbox! In most
circumstances,
| I don't think dynamically hiding UI elements is very nice (as opposed to
| just disabling them), but it's certainly another option!
|
| Ta
|
| | > Hi Stu,
| >
| > Thanks for your response.
| >
| > Yes, you're right. Also, I think your #2 option is a good idea, using
| > hidden field is my favorite , too :).
| > In addition, if you think it' ok that we makes the checkbox hidden
instead
| > of disabling it, we can try using clientside script to hide the checkbox
| > when we used to disable it. How do you think?
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| > --------------------
| > | From: "Stu Carter" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > | Subject: Re: Checkbox does not respect checked value if disabled
client
| > side
| > | Date: Fri, 30 Sep 2005 11:27:46 +0100
| > | Lines: 152
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: brooklyn-bridge.emea.veritas.com 62.172.234.2
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:11093
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi Steven,
| > |
| > | Thanks very much, I understand now.
| > |
| > | So since any control's state will not be posted back if it has been
| > disabled
| > | via client side script, the two options are:
| > |
| > | 1) Disable the checkbox in a postback.
| > | But that means a lot of postbacks when users are configuring other
| > options
| > | which isn't nice.
| > |
| > | 2) Add a client-side 'OnClicked' handler for the checkbox and always
| > record
| > | it's current state in a hidden TextBox that runs on the server (it
has
| > to
| > be
| > | hidden via client-side script and enabled!). Then in Page_Unload,
| > retrieve
| > | the true state of the CheckBox from the TextBox :)
| > |
| > | Give me a windows app any day!
| > |
| > | Cheers,
| > | Stu
| > |
| > |
| > | | > | > Hi Stu,
| > | >
| > | > Welcome to ASPNET newsgroup.
| > | > From your description, you're using the ASP.NET CheckBox server
| > control
| > on
| > | > your webpage and due to some reason, you use clientside script to
| > control
| > | > the checkbox's enable/disable at clientside(through the <input
| > | > type=checkbox..> 's "disabled" attribute ,yes?) . However , you
found
| > that
| > | > when the checkbox is disabled at clientside, when postback, the
| > checkbox's
| > | > "Enabled" and "Checked" property's value seems to be different from
| > your
| > | > expected value, yes?
| > | >
| > | > As for this problem, the behavior you've met is the expected one
due
| > to
| > | > the
| > | > ASP.NET server control's control model and state persistent
mechanism.
| > | > For
| > | > asp.net webserver control, the Enabled and other propeties like
| > "Checked"
| > | > for checkbox are all serverside properties which is active only
during
| > the
| > | > asp.nte page(or control)'s serverside lifecycle, after the page has
| > been
| > | > output to client(page lifecycle end), these properties be default
are
| > | > persisted into page's ViewState(if enabled), and then, the ViewState
| > won't
| > | > be modified at clientside (that's why clientside script's
modification
| > on
| > | > clientside html element won't affect the control's serverside
| > propertis
| > | > value). So this explain why the "Enabled" property is not as you
| > expected
| > | > when you modify the "disabled" attribute for the clientside element.
| > | >
| > | > AS for the "Checked" property, it is also related to another fact,
the
| > | > ASP.NET checkbox control's "Checked" property is populated through
the
| > | > following steps when page postback,
| > | >
| > | > 1. it will check the client post message's form collection to see
| > whethe
| > | > the clientside <input type=checkbox...> element's value is posted,
if
| > so,
| > | > it is marked as the new value, also, the old value from the
ViewState
| > is
| > | > also retrieved, and if they're different, the new value will
replace
| > the
| > | > old value. However, when you disabled the checkbox at clientside,
the
| > | > value won't be posted to server in the http message (this is the
same
| > for
| > | > other html form elemetn such as <input type=text ...> ...).
| > | > That's why when you disable the checkbox at clientside, you won't
get
| > the
| > | > updated checkbox's state at serverside.
| > | >
| > | > In addition, here are some msdn reference on asp.net servercontrol's
| > | > lifecycle model and ViewState:
| > | >
| > | > #Control Execution Lifecycle
| > | >
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
| > | > fecycle.asp?frame=true
| > | >
| > | > #Understanding ASP.NET View State
| > | >
| >
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
| > | > /viewstate.asp
| > | >
| > | > Hope helps. If you have anything unclear, please feel free to post
| > here.
| > | >
| > | > Thanks,
| > | >
| > | > Steven Cheng
| > | > Microsoft Online Support
| > | >
| > | > Get Secure! www.microsoft.com/security
| > | > (This posting is provided "AS IS", with no warranties, and confers
no
| > | > rights.)
| > | >
| > | >
| > | >
| > | >
| > | >
| > | >
| > | > --------------------
| > | > | From: "Stu Carter" <[email protected]>
| > | > | Subject: Checkbox does not respect checked value if disabled
client
| > side
| > | > | Date: Thu, 29 Sep 2005 13:58:08 +0100
| > | > | Lines: 34
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| > | > | X-RFC2646: Format=Flowed; Original
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| > | > | Message-ID: <[email protected]>
| > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | > | NNTP-Posting-Host: brooklyn-bridge.emea.veritas.com 62.172.234.2
| > | > | Path:
| > TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | > | Xref: TK2MSFTNGXA01.phx.gbl
| > | > microsoft.public.dotnet.framework.aspnet.webcontrols:11072
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | > |
| > | > | Hi,
| > | > |
| > | > | ENV: Windows 2003 Server, IE 6, .Net 1.1 SP1, VS 2003
| > | > |
| > | > | We have an ASP.Net server side Checkbox on a web page. The
checkbox
| > is
| > | > | disabled by some client-side JavaScript due to user interaction
with
| > | > some
| > | > | other controls on the page (i.e. the checkbox option is no longer
| > | > | applicable). We do this with JavaScript because a postback looks
| > poor
| > | > and
| > | > | takes time.
| > | > |
| > | > | This works fine. On Page_Unload, we want to store the last value
of
| > the
| > | > | checkbox in Session state, so we can reset the state when the user
| > | > returns
| > | > | to this page later.
| > | > |
| > | > | However, on a postback, the properties of the disabled checkbox
when
| > it
| > | > is
| > | > | checked are:
| > | > | Enabled = true
| > | > | Checked = false
| > | > |
| > | > | So, the Enabled state is wrong/hasn't been changed, but the
checked
| > | > state
| > | > | has been changed and is wrong! The control properties should be:
| > | > | Enabled = false
| > | > | Checked = true
| > | > |
| > | > | Can you please let me know why this is, and is there a fix?
| > | > |
| > | > | I know that using an HTML checkbox and running it server side is
an
| > | > option,
| > | > | but you then can't have a label alongside the checkbox that
triggers
| > | > | checking/unchecking the checkbox...sigh.
| > | > |
| > | > | Thanks,
| > | > | Stuart
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top