Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 2 of 2

Thread: Get current Category ID

  1. #1
    ajwaka is offline Junior Member
    Join Date
    Nov 2006
    Posts
    23

    Default Get current Category ID

    I've seen this asked in other threads - but their solutions don't work for me
    (http://forums.aspdotnetstorefront.co...-in-xmlpackage)

    I've turned on debugging but the querystring values are blank and I dont' see anything just showing the category id.

    For instance the url is c-7-ac-coolers.aspx - and I need to get the 7. What I'm trying to do is simple - show in a menu the categories - and if it is the current category - show the products for it. The only thing keeping me from this is getting the current category ID (in this case 7)

    I'm good in .net --- but xsl is a different story. Thanks for any help you can give me!

  2. #2
    ChrisAtAbilityBusiness is offline Junior Member
    Join Date
    Jun 2011
    Location
    NE Ohio
    Posts
    23

    Default

    Hi,

    You can do this by parsing the URL from an XSLT Extension function. It is not possibly the prettiest solution, but it works.

    Step 1
    In your web.config, locate the <xsltobjects node and add a line, as such:
    HTML Code:
    <xsltobjects defaultExtension="">
      <extensions>
        <clear />
        <add name="receipt" namespace="urn:receipt" type="ReceiptXsltExtension, app_code" />
        <add name="my" namespace="urn:my" type="my_CustomXSLTExtensions, app_code" />
      </extensions>
    </xsltobjects>
    Step 2
    At the top of your XmlPackage, edit the Namespace line to look like this:
    HTML Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="urn:aspdnsf" xmlns:my="urn:my" exclude-result-prefixes="aspdnsf my">
    Step 3
    Add a new class file to the App_Code folder called 'my_CustomXSLTExtensions.cs'. In that class, add the following method:
    Code:
    public static int GetCurrentCategoryID()
    {
      try
      {
        string _url = HttpContext.Current.Request.Path;
        if (_url.Contains("/c-"))
        {
          _url = _url.Replace("/c-", "");
          return Convert.ToInt32(_url.Substring(0, _url.IndexOf("-")));
        }
        else if (_url.Contains("/p-"))
        {
          _url = _url.Replace("/p-", "");
          return GetCategoryID(Convert.ToInt32(_url.Substring(0, _url.IndexOf("-"))));
        }
        else
        { return 0; }
      }
      catch { return 0; }
    }
    Step 4
    Call your new function from within your XmlPackage like so:
    HTML Code:
    <xsl:value-of select="my:GetCurrentCategoryID()" />
    Hope this helps!

    EDIT:
    In the above C# method, you will notice that if it is a standard Category page (ex. /c-...) then the CategoryID is parsed out and returned. If it is a standard Product page (ex. /p-...) the ProductID is parsed out and passed to a GetCategoryID method that simply queries the database to get the Category that this Product belongs to. If I recall correctly, this particular site the Products were mapped in one Category only, so this was sufficient. Obviously you can tailor the C# to your specific needs.
    END EDIT:
    Last edited by ChrisAtAbilityBusiness; 05-15-2012 at 06:48 PM.
    Christopher Hallam
    Business Application Development & Consulting
    Ability Business
    AspDotNetStorefront Preferred Development Partner/Reseller