Update 2013-01-27: the problem described in this post is reproducible in IE9. In other browsers when you add disabled attribute to the Checkbox, user still may change the value and bug is not reproduced.
Some time ago I faced with interesting behavior of ASP.Net Checkbox control, which confused me a bit. And I decided to dig into the problem in order to understand why it works like this. Consider the following example:
So we have 2 controls: one checkbox and one button. Button is needed only for testing postbacks, so we won’t pay attention to it. Checkbox has 2 important properties: one is Checked=”True” and another disabled=”disabled” attribute. I.e. we want to stay it checked and not allow users to change the value. When the page with these controls is requested first time (IsPostback = false), it looks like this:
Everything looks correct. But if we will click button (IsPostback = true), then it will look like this:
I.e. Checked property became false after postback. Why it happens?
The problem is in disabled=”disabled” attribute. According to html spec controls which have disabled attribute are “unsuccessful” controls (see here), which values are not included to the form post data. Ok, let’s remember it and try to run the following test:
It is almost the same, but except disabled attribute we used server property Enabled=”false”. In this case when we will request page first time we will get the following result:
Checkbox is still checked and user still can’t change its value, but now label is not greyed out. If we will click Postback button now, picture will remain the same:
In this case Checked property was not reset to the false. The interesting things will start when you will check generated html in second example:
So under the scene, input control anyway has disabled attribute, but its checked property is not lost when we use server property. The question why it happens interested me, so I made some investigation. Such cases is very good chance to download ASP.Net sources and debug it.
Let’s check what happens in control’s lifecycle in both examples. As you probably know when page is requested first time ASP.Net creates class in "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\webapplication\" folder – inheritor of the class which is used as codebehind for your page. Default values are set in this class: it has overridden FrameworkInitialize function (which is called right after constructor in lifecycle) which calls @__BuildControlchk function:
FrameworkInitialize > @__BuildControlTree > ... > @__BuildControlchk
Function @__BuildControlchk looks like this:
As you can see Checked property is set to true on line 7. And disabled attribute is added on line 8. Now is turn to investigate the code of Checkbox control. You can do it in some decompiler or you can enable Microsoft Symbols Server in Tools > Options > Debugging > Symbols in VS and enable debug of .Net code:
- Uncheck "Enable Just My Code (Managed only)"
- Check "Enable source server support"
- Uncheck "Require source files to exactly match the original version"
First of all we are interesting in OnPreRender method:
I removed the code which is not important for our investigation. On line 7 it calls SaveCheckedViewState function in order to determine should save Checked property into ViewState or not:
I leaved comments here from the source code of the Checkbox control which say that it is done for optimizing the size of the view state. Debugger showed that in first example (when we use disabled attribute) SaveCheckedViewState returns false from line 30. IsEnabled in the first example is true, because we didn’t set it to true explicitly. It means that in Checkbox.OnPreRender method the following call will be made:
It actually marks property as not dirty, but in result it means that it won’t be included into the view state. Check the code of StateBag.SaveViewState:
As you can see only items which have IsDirty = true are stored into the list of view state objects (lines 11-19). For us all of this means that in first example Checked property is not stored to the ViewState of the control for optimizing the size of the view state, according to the comments. Checked property is retrieved from the view state:
As in first example it is removed from the view state, every call to get_Checked will return false. This is the reason of losing the Checked property in first example. During postback Checkbox.LoadPostData method is called:
As you can see it gets value from the post values collection and if it is empty, isChecked will be false. This is what happens in first example, because as I wrote above controls with disabled attribute are not posted. This method is called after OnInit and before OnLoad method, so e.g. in OnInit method Checked is still true (because we have default value initialization in temporary ASP.Net class – see above), but in OnLoad it will be false because of this LoadPostData call.
Now what happens in second example when we use Enabled = false property? In this case SaveCheckedViewState method returns true which means that Checked will be stored in view state. But as we saw above when we have Enabled = false, disabled attribute is also added. It is added in Checkbox.RenderInputTag method:
So in this case value also is not included to the post data, but why then Checked doesn’t become false in Checkbox.LoadPostData method like in first example?
The answer is because Checkbox.LoadPostData method is not called at all in second example. Let’s check the code of the Page.ProcessPostData method which calls LoadPostData methods from child controls:
I.e. page calls this method only for controls from _controlsRequiringPostBack collection. Let’s check Checkbox.OnPreRender method again:
I.e. checkbox is added to _controlsRequiringPostBack collection only when it is Enabled. If Enabled = false, postback data is not even loaded for it.
The summary of the investigation is that Enabled=”false” will cause disabled=”disalbed” attribute, but not vice versa. I hope that this article will help you to understand the difference between Checkbox.IsEnabled property and disabled attribute. It also shows that sometimes it is useful to download ASP.Net code and investigate it in the debugger.