locked
Get value from ASP.NET checkbox RRS feed

  • Question

  • User-1934530859 posted

    I don't believe ASP checkboxes have a native value assigned to them. How do I assign and retrive a value from a ASP.NET checkbox.

    Friday, November 6, 2009 12:20 PM

Answers

  • User176332441 posted

    you are not kidding right

    there is no html tag like

    <checkbox value="5" checked> 

    if you meant just simple

    <input id="Checkbox" type="checkbox" name="mycheckbox" value="5"/>

    then u can get the value using forms collection

    label_Result.text = Request.Form["mycheckbox"]; 

    and you will get the value 5 only if that checkbox is checked 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 6, 2009 6:17 PM

All replies

  • User-837620913 posted

    You can get the value using the IsChecked property:

    bool didYouCheckTheBox = myCheckbox.IsChecked;  // returns true or false


    The default value is false, unless you specifically set the checkbox to be checked in the markup or in the code behind.

    Friday, November 6, 2009 12:27 PM
  • User-1934530859 posted

    no I mean to have a seperate value assigne to a checkbox like a standard HTML checkbox ie:

    <checkbox value="5" checked>

    Friday, November 6, 2009 1:00 PM
  • User510245635 posted

    Hi,

    what I do in this case, I write a value to a hidden field in the when-check-box-changed trigger, like this:

    if mycheckbox.checked then

      myhiddenfield.value = 5

    else

      myhidenfield.value = 4

    end if

    Hope it helps

    Regards

    Dieter


    Friday, November 6, 2009 6:02 PM
  • User176332441 posted

    you are not kidding right

    there is no html tag like

    <checkbox value="5" checked> 

    if you meant just simple

    <input id="Checkbox" type="checkbox" name="mycheckbox" value="5"/>

    then u can get the value using forms collection

    label_Result.text = Request.Form["mycheckbox"]; 

    and you will get the value 5 only if that checkbox is checked 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 6, 2009 6:17 PM