Calling Custom Fields

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
BWalt302
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 23
Joined: Mon Oct 17, 2011 10:21 am

Calling Custom Fields

Post by BWalt302 » Thu Dec 22, 2011 12:09 pm

OK So I have a custom field set up called FAQ. It works and saves information in the field from the admin side, thats not a problem.

What I need to do is create a new file, FAQ.ascx & FAQ.ascx.cs which is a conlib file that will call only from this FAQ custom field, it cannot call all of the fields because some of the custom fields I have are "private" admin only fields.

I would like to be able to use <asp:Literal ID="FAQ" runat="server" /> something like that <--- to print the FAQ custom field information into the Tabs plugin I have installed. Any help would be greatly appreciated, thanks!

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Calling Custom Fields

Post by david-ebt » Thu Dec 22, 2011 6:23 pm

Take a look at this forum post:
viewtopic.php?f=44&t=14529

It has an example of adding a custom field to the featuredproducts control. You should be able to use that as a template for creating a new control that just displays the FAQ field.
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

BWalt302
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 23
Joined: Mon Oct 17, 2011 10:21 am

Re: Calling Custom Fields

Post by BWalt302 » Tue Dec 27, 2011 9:10 am

Hey, so I tried to do this, and am getting :
[[Conlib:Custom\CallFAQ]] d:\FireFold\ConLib\Custom\CallFAQ.ascx.cs(1): error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

As an error.

My Codes are below, any suggestions?

Code: Select all

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommerceBuilder.Common;
using CommerceBuilder.DigitalDelivery;
using CommerceBuilder.Orders;
using CommerceBuilder.Products;
using CommerceBuilder.Utility;

namespace CallFAQ	  
{  
    public class MyFAQ  
    {  
    protected string GetCustomField(object dataItem, string fieldName)
    {
        Product product = (Product)dataItem;
        foreach (ProductCustomField pcf in product.CustomFields)
        {
            if (pcf.FieldName == fieldName)
                return pcf.FieldValue;
        }
        return string.Empty;
    }
	    protected string GetCustomField(object dataItem)
    {
        Product product = (Product)dataItem;
        string customValues = string.Empty;

        foreach (ProductCustomField pcf in product.CustomFields)
        {
            if (pcf.FieldName == "FAQ")
            {
                customValues += string.Format("Freq. Asked Questions = {0}<br />", pcf.FieldValue);
        }
        return customValues;
    }
	}
}
}

Code: Select all

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CallFAQ.ascx.cs" Inherits="Webparts_FeaturedProductsGrid" EnableViewState="false" %>
<%--
<conlib>
<summary>Displays featured items in a category.</summary>
<param name="Caption" default="Featured Items">Possible value can be any string.  Title of the control.</param>
<param name="Size" default="3">Possible value cab be any integer greater then zero. Indicates that at maximum how many items can be shown.</param>
<param name="Columns" default="2">Possible value cab be any integer greater then zero. Indicates that the grid will contain how much columns.</param>
<param name="IncludeOutOfStockItems" default="false">Possible values be true of false. Indicates that the grid will display out of sctock items or not.</param>
<param name="ThumbnailPosition" default="LEFT">Possible values are 'TOP' or 'LEFT'.  Indicates whether the product image will be displayed on top of product details or on the left.</param>
<param name="Orientation" default="HORIZONTAL">Possible values are 'HORIZONTAL' or 'VERTICAL'.  Indicates whether the contents will be displayed vertically or horizontally, In case of vertical orientation only one column will be displayed.</param>
</conlib>
--%>

                    <div style="margin-top:10px"><asp:Label ID="FAQ" runat="server" Text='<%# GetCustomField(Container.DataItem, "CustomField") %>'></asp:Label></div>

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Calling Custom Fields

Post by david-ebt » Tue Dec 27, 2011 2:12 pm

Based on your code, it looks like you're trying to place this as a separate control on your product page. Here is a conlib control stripped down to the basics that will display just the FAQ custom field for a product when the control is placed somewhere on the product page.

CallFAQ.ascx:

Code: Select all

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CallFAQ.ascx.cs" Inherits="ConLib_FAQ" EnableViewState="false" %>
<%--
<conlib>
<summary>Displays Product FAQ.</summary>
</conlib>
--%>

<div style="margin-top:10px"><asp:Label ID="FAQ" runat="server" Text=''></asp:Label></div>
CalFAQ.ascx.cx:

Code: Select all

using System;
using CommerceBuilder.Products;
using CommerceBuilder.Utility;

public partial class ConLib_FAQ : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int _ProductId = AlwaysConvert.ToInt(Request.QueryString["ProductId"]);

        ProductCustomFieldCollection customFields = ProductCustomFieldDataSource.LoadForProduct(_ProductId);
        string customValues = string.Empty;

        foreach (ProductCustomField pcf in customFields)
        {
            if (pcf.FieldName == "FAQ")
                customValues += string.Format("Freq. Asked Questions = {0}<br />", pcf.FieldValue);
        }

        FAQ.Text = customValues.ToString();
    }
}
The example from the featuredproducts control in the forum topic referenced assumed that the FAQ field was inside another .NET container like a REPEATER or GRID.

Hopefully this helps,
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

BWalt302
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 23
Joined: Mon Oct 17, 2011 10:21 am

Re: Calling Custom Fields

Post by BWalt302 » Tue Dec 27, 2011 5:04 pm

That worked like a charm, added it to my Tabs code, and pulled it with "<bmw:FAQ ID="FAQ" runat="server" />"

Thank you so much for the help!

RKS_213
Lieutenant (LT)
Lieutenant (LT)
Posts: 69
Joined: Mon Dec 26, 2011 2:48 pm

Re: Calling Custom Fields

Post by RKS_213 » Mon Jan 02, 2012 2:15 pm

The example from the featuredproducts control in the forum topic referenced assumed that the FAQ field was inside another .NET container like a REPEATER or GRID.
I have been trying to get this to work and I'm still getting the error mentioned above. I am assuming it is because I (don't?) have my field inside another .NET container. All I've done is add the new text field into the EditProducts.aspx and then I moved on to here and trying to get it to display on my product page.

RKS_213
Lieutenant (LT)
Lieutenant (LT)
Posts: 69
Joined: Mon Dec 26, 2011 2:48 pm

Re: Calling Custom Fields

Post by RKS_213 » Mon Jan 02, 2012 3:34 pm

Okay, in another thread someone mentioned I might be using an older version of AC. I have AbleCommerce 7.0.5 build 14053. Is this too old to use these tutorials here on the site?

All I ever get is Runtime errors. I followed the above posting EXACTLY just to see if there was anything relating to FAQs or anything. Nothing. Just a runtime error. I've followed a few tutorials here on the site and all I ever get are runtime errors.

Thanks.

User avatar
david-ebt
Captain (CAPT)
Captain (CAPT)
Posts: 253
Joined: Fri Dec 31, 2010 10:12 am

Re: Calling Custom Fields

Post by david-ebt » Mon Jan 02, 2012 4:39 pm

7.0.5 should be new enough. We've done this with 7.0.4.

What is the data in the repeater or grid control? Is it products or catalog nodes? The code below assumes the repeater or grid holds products.

To put the FAQ inside a repeater or a grid, add this code inside the repeater or grid.This code assumes that you will only be asking for the FAQ data.

Code: Select all

<div style="margin-top:10px"><asp:Label ID="FAQ" runat="server" Text='<%# GetCustomField(Container.DataItem) %>'></asp:Label></div>
Then put this function into the code file:

Code: Select all

protected string GetCustomField(object dataItem)
{
    Product product = (Product)dataItem;
    string customValues = string.Empty;

    foreach (ProductCustomField pcf in product.CustomFields)
    {
        if (pcf.FieldName == "FAQ")
            customValues += string.Format("Freq. Asked Questions = {0}<br />", pcf.FieldValue);
    }
    return customValues;
}
Some of the previous code assumed you were on a product page and therefore had the ProductID immediately available. This code should work inside a repeater or grid where you are displaying multiple products.
David
http://www.ecombuildertoday.com
Enhanced Reporting for AbleCommerce
Image

Post Reply