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

Thread: Product/Topic Page Code Behind

  1. #1
    jmcconahie is offline Junior Member
    Join Date
    Nov 2011
    Posts
    13

    Default Product/Topic Page Code Behind

    I need to run a function in the Page_Load() event of the code behind for Product and Content pages. These are pages that begin with "p-" and "c-" respectively. What files contain the code behind for these pages?

    For example, TopicControl.ascx has the code behind for Topic pages ("t-" pages). I need the equivalent for the other 2 pages mentioned above.

    I am running ML 8.

  2. #2
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    Hint - check the route table in the web.config - to be clear, TopicControl.ascx isn't a "page" it is a control that is loaded by the page ~/driver.aspx, Product pages (p-) are loaded by ~/showproduct.aspx adn category pages (c-) are loaded by ~/showcategory.aspx.

    If you want the code on every page, then I'd recommend actually using App_Code/SkinBase.cs or App_Code/MasterPageBase.cs - then you only have to write it once.

  3. #3
    jmcconahie is offline Junior Member
    Join Date
    Nov 2011
    Posts
    13

    Default

    Thanks for the response. I didn't see a Page_Load function in the SkinBase.cs file, so I'll probably just stick it in each of the 3 files I need the code behind for.

    Thanks again

  4. #4
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    You can add a Page_Load to SkinBase

  5. #5
    jmcconahie is offline Junior Member
    Join Date
    Nov 2011
    Posts
    13

    Default

    Now I'm having trouble accessing html controls from the code behind of "t-" pages. I added a div to a few topic pages (via admin tool Manage Topics) and declared it as follows:

    <div id="mydiv" runat="server"></div>

    now, this worked fine for "c-" pages. I used the FindControl() method to get the control and modify its innerHTML. But when I tried the same thing with "t-" pages, I get null. I used the "Inspect Element" tool in Chrome to look at the source after the page loaded and I saw a difference between the two pages. for "c-" pages, I saw:

    <div id="mydiv">The innerHTML I added</div>

    and for "t-" pages I saw:

    <div id="mydiv" runat="server"></div>

    This leads me to conclude that on "t-" pages, it isn't recognizing the runat=server part of the html and the control isnt getting added to the server side control tree. Because of this, I can't get it using the FindControl() method. How can I get around this issue?

  6. #6
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    runat="server" isn't valid HTML - it is valid ASP - the topics page is inserting your text verbatim

    what exactly are you trying to accomplish?

  7. #7
    jmcconahie is offline Junior Member
    Join Date
    Nov 2011
    Posts
    13

    Default

    I'm reading information from an XML file and inserting it into the page.

    When a user visits a page, I check to see how old the XML file is and if it's older than a certain amount of time, I re-download it from a web server (basically a file swap cache). I then load the information from the XML, set a string equal to some HTML generated using that info, and set the innerHTML of a div on the page equal to the generated HTML string.

    I just can't figure out how to access/set the innerHTML of a div on a CMS page from code behind. Putting runat="server" works for "c-" pages, but not "t-" pages.

    EDIT: here's really simplified pseudocode of what I want to accomplish:

    protected override void OnPreRender(EventArgs e)
    {
    //store code here

    //my code
    if (Page.FindControl("mydiv") != null)
    {
    string markup = "<div>HTML code string I build dynamically.</div>";

    ((HtmlContainerControl)FindControl("mydiv")).Inner Html = markup;
    }
    }
    Last edited by jmcconahie; 12-12-2011 at 01:57 PM. Reason: Added Pseudocode

  8. #8
    don.irwin is offline Member
    Join Date
    Apr 2006
    Location
    Phoenix AZ
    Posts
    81

    Default

    in driver.aspx.cs you might want to try grabbing Topic.Contents and then do a string.replace

  9. #9
    jmcconahie is offline Junior Member
    Join Date
    Nov 2011
    Posts
    13

    Default

    Thanks for the suggestion. I'll give it a try.

  10. #10
    jmcconahie is offline Junior Member
    Join Date
    Nov 2011
    Posts
    13

    Default

    Trying to access the Topic Contents in the driver.aspx.cs file gave me nothing. However, I was able to access it in the TopicControl.ascx.cs file's Page_Load event. I used something like:

    if(Contents.Text.Contains("<div id=\"myControl\"></div>") )
    {
    //build string
    string test = "blahblah";

    Contents.Text = Contents.Text.Replace("<div id=\"myControl\"></div>", "<div id=\"myControl\">" + test + "</div>");
    }


    It works like a champ. Thanks so much for the help!
    Last edited by jmcconahie; 12-13-2011 at 07:47 AM. Reason: Added Pseudocode