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 13 of 13

Thread: Controls fail in the template.ascx

  1. #1
    Web Design Magic is offline Junior Member
    Join Date
    Aug 2008
    Posts
    12

    Default Controls fail in the template.ascx

    Hi,

    I am trying to access new controls (like say a Literal) that I have added to the template.ascx page in the templatebase.cs page.

    When in the templatebase.cs code page the controls are said to be out of context.

    So what am I trying to do?

    Well I am trying to dynamically change the Href of a CSS link tag from the templatebase.cs.

    In the template.ascx page

    <link id="CSSLink" href="something.css">

    And in the TemplateBase.cs page

    if (Page.ThisCustmer.CustomerLevelID==1)
    {
    CSSLink.href = "whtever.css";

    }

    So in summary I cannot seem to operate controls between the two files. Any ideas??

    Thanks Daren

  2. #2
    George the Great is offline Senior Member
    Join Date
    Nov 2006
    Location
    Cleveland, OH
    Posts
    1,792

    Default

    There are a couple things here. First, because of the nature of our skinning engine (basically Masterpages before Masterpages existed), the controls do not yet exist in the page life cycle where you are trying to access them. A better place to do this would be in App_Code/SkinBase.cs...just add an OnInit override to the file and do your logic there
    Code:
    protected override void OnInit(EventArgs e)
    {
        Literal lTest = (Literal)this.FindControl("litTest");
        lTest.Text = "Text that I'm using to replace in template.ascx from SkinBase";
    
        base.OnInit(e);
    }
    Then my template.ascx file
    Code:
    <asp:Literal ID="litTest" runat="server">litTest</asp:Literal>
    litTest would be replaced with Text that I'm using to replace in template.ascx from SkinBase.

    The other thing that you'll need to remember is that if you plan on accessing controls in the template from a code-behind or class, that the control will need to have the runat attribute added...this will work
    Code:
    <asp:Literal ID="litTest" runat="server">litTest</asp:Literal>
    this will not
    Code:
    <asp:Literal ID="litTest">litTest</asp:Literal>
    <a href="http://www.aspdotnetstorefront.com">Shopping Cart Software</a>

  3. #3
    Web Design Magic is offline Junior Member
    Join Date
    Aug 2008
    Posts
    12

    Default

    Thankyou. This works very well..

  4. #4
    Web Design Magic is offline Junior Member
    Join Date
    Aug 2008
    Posts
    12

    Default One last thing!

    Hi,

    Thanks for that. It worked all good on the front and topic pages... http://119.161.34.77/

    But now when I goto a product page it appears the Literal Control is not changed. (It is blank in the CSS Href field. Like it is not getting created??

    http://119.161.34.77/c-7-car-vaporizer.aspx

    So I have to ask the question does the protected override void OnInit(EventArgs e) fire on products or categories??

    Thanks Daren

  5. #5
    George the Great is offline Senior Member
    Join Date
    Nov 2006
    Location
    Cleveland, OH
    Posts
    1,792

    Default

    First, it looks like you're using different templates for the home page vs. the category page correct? Your category page template has the custom stylesheet link wrong, so the page isn't styled. Looks like
    Code:
    <link rel="stylesheet" type="text/css" href="assets/css/" />
    needs to be
    Code:
    <link rel="stylesheet" type="text/css" href="assets/css/layout_Retail.css" />
    Next, I'm going to assume that Home Content is the control you're populating from SkinBase. If so, you have the control populated in the content area, which is going to be replaced on any page that uses the content area to display its data (product/entity pages). Just move it outside the content area in the template and it should work.
    <a href="http://www.aspdotnetstorefront.com">Shopping Cart Software</a>

  6. #6
    Web Design Magic is offline Junior Member
    Join Date
    Aug 2008
    Posts
    12

    Default still got problems

    Hi George,

    No we are only using one template skin_1/template.ascx

    Also I think your off track with the Home Content.

    Basically I am changing a Literal Control in the skinbase.cs file

    skinbase.cs

    protected override void OnInit(EventArgs e)
    {
    Literal LitCSSPath = (Literal)this.FindControl("CSSPath");
    // test to see what Customer Level the customer is on
    if (LitCSSPath!=null){
    if (m_ThisCustomer.CustomerLevelName == "Wholesale")
    {
    LitCSSPath.Text = "layout_Wholesale.css";
    }
    else
    {
    LitCSSPath.Text = "layout_Retail.css";
    }
    }

    template.ascx

    <link rel="stylesheet" type="text/css" href="assets/css/<asp:Literal ID="CSSPath" runat="server"></asp:Literal>" />

    The problem is that it works on the home and topic pages, but not on products. I beleive that on products/categories the code above cannot find the CSSPath control so it is not setting it at all. Hence the reason for the missing layout_Retail.css.

    Daren

  7. #7
    mc9000 is offline Member
    Join Date
    Aug 2007
    Posts
    83

    Default

    I have the same issue.
    What's happening is that the control can no longer be referenced by it's name after the first time it is rendered, so, as soon as you go to another page, the control becomes nothing - making it impossible to reference again. It does not matter what the control is (label, textbox, custom, etc. ).

    Maybe the cache is to blame. If that's the case, turning off the cache appears to be the only solution. Bummer!

  8. #8
    BFG 9000 is offline Senior Member
    Join Date
    Oct 2006
    Location
    South UK
    Posts
    882

    Default

    Why not use an xmlpackage to write your css tag?

    e.g. (in psuedo code) :-

    if customerlevel = 1 then whatever.css
    if customerlevel = 2 then whatever2.css
    else whatever3.css

    then simply call the xmlpackage from your template.


    TTFN

    BFG

  9. #9
    mc9000 is offline Member
    Join Date
    Aug 2007
    Posts
    83

    Default

    The real issue here is that you can't use any asp.net controls via the template.ascx on any entity pages. Tried turning off the cache to no avail. This is not good.
    There MUST be a way!!!!

  10. #10
    BFG 9000 is offline Senior Member
    Join Date
    Oct 2006
    Location
    South UK
    Posts
    882

    Default

    No, the real issue is that you can't buy a relatively inexpensive bit of software that will do exactly what everyone wants.

    I'm fairly sure that pretty much anything can be done in an xmlpackage, or, if that's not good enough for you, then the source is available so you can modify anything & everything.


    TTFN

    BFG

  11. #11
    mc9000 is offline Member
    Join Date
    Aug 2007
    Posts
    83

    Default

    Here's the test code I tried (works only once, unfortunately) -
    if anyone knows how to get this to work every time - that would help out anyone trying to use asp.net controls (to me this is the most basic need of any shopping cart written in asp.net - not asking for a special situation)


    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)

    Dim lbltest As Label = DirectCast(Me.FindControl("lblTest"), Label)
    If Not lbltest Is Nothing Then
    lbltest.Text = "This only works once"
    End If

    MyBase.OnInit(e)

    End Sub

  12. #12
    Web Design Magic is offline Junior Member
    Join Date
    Aug 2008
    Posts
    12

    Default One Last Question..

    Hi,

    I am trying to get the Customer Level ID using XSLT.

    Does the below look correct??

    HTML Code:
    <?xml version="1.0" standalone="yes" ?>
    <package version="2.1" displayname="Claims Link" debug="false">
    
        <PackageTransform>
            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="urn:aspdnsf" exclude-result-prefixes="aspdnsf">
    	        <xsl:output method="html" omit-xml-declaration="yes" />
                <xsl:template match="/">
                    <xsl:choose>
                        <xsl:when test="/root/Runtime/CustomerLevelID = '2'">
    								<xsl:value-of select="layout_Wholesale.css" />
    					</xsl:when>
                        <xsl:otherwise>
    								<xsl:value-of select="layout_Retail.css" />
    </xsl:otherwise>
                    </xsl:choose>
                </xsl:template>
    
            </xsl:stylesheet>
        </PackageTransform>
    </package>

  13. #13
    mc9000 is offline Member
    Join Date
    Aug 2007
    Posts
    83

    Smile figured out a better way

    Got it figured out:
    Forget about the controls (and all that mess), just create an ASPX page (not a control), remove the html,body,header, etc tags (treat it like a ASCX control), put a form tag in with a unique id. on the code behind, inherit SkinBase on the code behind:

    'Inherits System.Web.UI.Page 'this is replaced
    Inherits SkinBase




    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="NewSearch.aspx.vb" Inherits="NewSearchPage" %>
    <%@ Register Src="ctrls/NewSearchCriteria.ascx" TagName="SearchCriteria" TagPrefix="uc1" %>

    <div id="NewSearchPage">
    <form id="form1" runat="server">
    <h3>
    Search For Products</h3>
    ...
    page with controls and what-have you
    ...
    </form>
    </div>



    That was it. Work's like a charm - at least for now (I have not tested this hack on MS AJAX yet). Too bad we purchased this in December - I would have liked to have gotten version 9 (that supports MasterPages) for the same price - (like buying the last model of a new car, then a better model comes out the day after you drive your "new" car off the lot ).