Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kevon K. Hayes 255 posts 281 karma points
    Feb 17, 2011 @ 16:41
    Kevon K. Hayes
    0

    How to Check if Checkbox list value is checked

    I have a checklist box with one value in it, and depending if that box is checked or not, I want to output a string.  How could I check if the box is checked or not... in .NET or XSLT doesn't matter. 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 17, 2011 @ 16:53
    Tom Fulton
    0

    Hi Kevon,

    Could you give us a bit more info?

    Is the checkbox on the website or the back end?  Assuming website since you mentioned XSLT.

    How are you rendering the checkbox?

    Is the checkbox inside a form that you are posting?

    If so, you can use XSLT with the umbraco.library helper to query the Request collection, something like:

    <xsl:variable name="checkbox" select="umbraco.library:RequestForm('id-of-your-checkbox')"/>
    <xsl:if test="string($checkbox) != ''">
      your string
    </xsl:if>

    If you are not posting the form you might need to use some jQuery, something along the lines of:

        $('#id-of-checkbox').click(function () {
            if ($(this).is(':checked')) {
                $('#id-of-string-container').html("your string");
            }
            else {
            // do something else
            }
        });

    -Tom

  • Kevon K. Hayes 255 posts 281 karma points
    Feb 17, 2011 @ 18:36
    Kevon K. Hayes
    0

    Thanks Tom,

    I created an Umbraco Datatype from the Dev Section of type checkbox list with one value, and then assigned that type to a property on my doctype. Nothing custom just all Umbraco here.  I know how to check if a checkbox is checked the general .NET way just don't know if same rules apply here.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 17, 2011 @ 18:44
    Tom Fulton
    0

    Are you running this check from the backend or the website and where are you wanting to display this string?

    If from the backend, you could get it's value with the Document API - the I think the property value should be empty if unchecked

    Document d = new Document(Convert.ToInt32(HttpContext.Current.Request["id"]));
    if (d.getProperty("aliasofcheckboxproperty") != null && !String.IsNullOrEmpty(d.getProperty("aliasofcheckboxproperty").Value.ToString()))
    {
    //write out your string...
    }

    Or from the website you could of course use XSLT

    <xsl:if test="string($currentPage/aliasofcheckboxproperty) != ''">
    write out your string
    </xsl:if>

    Also would it be easier to use a True/false (single checkbox) field or is there a reason you are using a CheckboxList?

    -Tom

  • Kevon K. Hayes 255 posts 281 karma points
    Feb 17, 2011 @ 18:55
    Kevon K. Hayes
    0

    Thanks Tom,

    For some reason I just thought it would be a different process because the datatype was a checkbox.

Please Sign in or register to post replies

Write your reply to:

Draft