3

I have a problem with checkbox. For example, I have this aspx's code

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
     <asp:CheckBox ID="chkActive" AutoPostBack="True" OnCheckedChanged="Active_OnCheckedChanged" runat="server"></asp:CheckBox>
     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</asp:Content>

At server side, when chkActive is checked, I have two rendered controls:

  • ctl00$MainContent$chkActive
  • ctl00$MainContent$txtName

when chkActive is unchecked, I have only one rendered control:

  • ctl00$MainContent$txtName

So I cannot write the status of that checkbox to database. For the record, I use a SaveData function with dynamic object, and chkActive disappeared when unchecked, so this function couldn't find it. Any idea to make checkbox appeared even when it's unchecked? Thanks.

P.S: "disappeared" means it's not in Request.Form anymore when unchecked.

Edited:

My SaveData function:

public void SaveDate<T>(T entity, NameValueCollection attributes)
    {
        PropertyInfo[] properties = typeof(T).GetProperties();
        foreach (PropertyInfo property in properties)
        {
            if (attributes.AllKeys.Any(key => key.Contains("$" + property.Name)))
            {
                //
            }
        }            
    }

The para attributes is from Request.Form, since checkbox is not in Request.Form anymore, so I don't know how to handle it. This function works well in case checkbox is checked

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74

3 Answers3

1

ASP.NET uses the native form abilities of browsers to return the data to the server.

When you "check" a checkbox, the data is passed back as part of the Request.Form data, and ASP.NET can then use this information as part of it's post-back life cycle to know the checkbox was "checked".

But if the checkbox is not "checked", it does not have an entry in the Request.Form data - and it's this lack of data which ASP.NET uses to know that the checkbox is no longer checked.

This is normal behaviour for checkboxes as part of form posting.

freefaller
  • 19,368
  • 7
  • 57
  • 87
  • Hi thanks, now I understand more about the behaviour of checkboxes. Do you know any solution or work-around to handle checkbox when it is not checked? Thanks – Ragnarsson Sep 10 '12 at 12:25
  • @Qui, I'm not 100% sure I understand what you mean by "handle checkbox". Are you talking on the server-side or client-side? As Dai and Chris have answered, you can check the `.Checked` property of the control... but this will only work once the form properties have been processed in the life-cycle, in other words in or after the Page_Load – freefaller Sep 10 '12 at 12:29
  • Thanks, I got what they meant. I edited my question, so maybe it's clearer what I meant. – Ragnarsson Sep 10 '12 at 12:45
1

As others have mentioned, the value of the checkbox (unchecked) is not sent back to the server, so it will not appear in the Request.Form collection.

But... in your function, use can use the server-side objects because the value IS posted back in ViewState (provided you have it on).

this.chkActive.Checked should be false on postback when unchecked on the client and submitted.

If you still want to use Request.Form, you can always check to see if the entry exists in the collection. If it is not there, the checkbox was false, or unchecked.

if(Request.Form["chkActive"] == null)
  // checkbox is unchecked
else
  // checkbox is checked

EDIT

To make the checkbox appear in the Request.Form collection, simply add a hidden input to your form and give it the same name as the checkbox.

<input type="hidden" value="false" name="chkActive" />

Add a client-side event handler to the OnClick event of the checkbox. When checked, set the hidden input's disabled property to true. When unchecked, set the disabled property to false. Disabled controls are not sent back to the server just like unchecked checkboxes, so if the checkbox is checked, the input should be disabled, and visa-versa.

Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
  • I edited my question, maybe you'll find it clearer what I meant. Thanks – Ragnarsson Sep 10 '12 at 12:47
  • I did as you suggested,
    ``
    And then I used
    `document.getElementById("hiddenExtern").disabled = true | false`
    but it didn't work, because disabled property does not work with type hidden. Thanks anyway.
    – Ragnarsson Sep 10 '12 at 15:21
  • @QuiTran - hidden input fields DO support the disabled attribute. Check this post - http://stackoverflow.com/questions/1374147/how-to-avoid-sending-input-fields-which-are-hidden-by-displaynone-to-a-server – Chris Gessler Sep 10 '12 at 17:30
  • Hi, I made it work. I still used the input hidden and I handled its name in my SaveData function to save checkbox's state to DB. Thanks for your help :) – Ragnarsson Sep 11 '12 at 10:38
0

Checkboxes have no value when they aren't checked, so the browser does not include them in the fields that are returned. This has nothing to do with ASP.NET, but rather how HTML forms work.

However, ASP.NET WebForms control checkboxes are aware of their state (thanks to viewstate) and will indicate if they're checked or not, just query the checkbox object directly instead of using the Request.Form collection, like so:

if( this.chkActive.Checked ) { ...
Dai
  • 141,631
  • 28
  • 261
  • 374