Copied to clipboard

Flag this post as spam?

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


  • Rich Green 2246 posts 4008 karma points
    Mar 20, 2010 @ 00:00
    Rich Green
    0

    Looping through form and checkboxes?

    Hi,

    Is there a way in XSLT to loop through a bunch of checkboxes  and return just the checked ones using umbraco.library:Request?

    The checkboxes are dynamically generated so I do not know their names.

    Many Thanks,

    Rich

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 20, 2010 @ 00:26
    Lee Kelleher
    1

    Hi Rich,

    As far as I'm aware, there isn't a way using the umbraco.library XSLT extensions to do this.  You'd need to know the name of the item.

    If you are able to write a custom XSLT extension, here's a bit of code that you might find useful?

    public static XPathNodeIterator GetRequestForm()
    {
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(String.Concat("<Request.Form />"));
    
        NameValueCollection nvc = HttpContext.Current.Request.Form;
    
        if (nvc != null)
        {
            try
            {
                for (int i = 0; i < nvc.Count; i++)
                {
                    XmlNode node = xmlHelper.addCDataNode(xd, nvc.GetKey(i), nvc.Get(i));
                    xd.DocumentElement.AppendChild(node);
                }
            }
            catch (Exception ex)
            {
                xd.DocumentElement.AppendChild(xmlHelper.addTextNode(xd, "error", ex.Message));
            }
        }
        else
        {
            xd.DocumentElement.AppendChild(xmlHelper.addTextNode(xd, "error", String.Concat("The Request.Form object is empty.")));
        }
    
        return xd.CreateNavigator().Select("/");
    }

    This will return an XML node-set that you can loop through in your XSLT.  From there you can check if the checkbox value is "on".

    You can customise the above code snippet to use any of the Request objects that inherit from a NameValueCollection - like Request.QueryString or Request.ServerVariables, (unfortunately Request.Cookies inherits from HttpCookieCollection, so can't be used here).

    Good luck, Lee.

  • Rich Green 2246 posts 4008 karma points
    Mar 20, 2010 @ 12:00
    Rich Green
    0

    Hey Lee,

    Thanks so much for your reply, looks like what I need! 

    Will let you know how I get on when I try to implement it.

    Cheers

    Rich

Please Sign in or register to post replies

Write your reply to:

Draft