The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

asp.net masterpages! getting IDs from subpages

flatlinegraphicsflatlinegraphics Registered User regular
edited July 2009 in Help / Advice Forum
OK, i am mostly a php person, with a smattering of classic asp. but the current project I am on is a .net project based on masterpages.

ok, masterpages, cool. kind of like a super template system. abstract away all the structure from the content. awesome.

small snag. i need to communicate a variable back to the masterpage from the content page.

I can pull the file name from the page
string[] pagename = System.IO.Path.GetFileName(Page.Request.PhysicalPath).ToLower().Split('.');

but i'd really like to get the content id
<asp:Content ID="subpage" ContentPlaceHolderID="mainContent" Runat="Server">

the basic idea is that i am setting the body class on a per page basis so that I can set css sprites, over, and here states.

anybody have any ideas? is this possible?

flatlinegraphics on

Posts

  • SoulStalkerSoulStalker Registered User regular
    edited July 2009
    Since each subpage is a separate class, you can use the MasterPage's Page property to determine which page you're currently on by checking it's Type (Page.GetType() from your MasterPage). If you add a reference to that page from your MasterPage (e.g. - <%@ Reference Page="~/Default.aspx" %>), you can access all the public variables/properties of that Page object by casting the "Page" to that type.

    Does that help?

    SoulStalker on
    Misanthropist extraordinaire.
  • flatlinegraphicsflatlinegraphics Registered User regular
    edited July 2009
    hm. do i then need to list each page as a <%@ Reference %>?

    basically, what i am trying to replicate is in php:

    <? $pagename = "about";
    include("header.php");
    ?>
    content blah blah blah


    where in the header.php file is all the templated html gubbins, with outputs of the $pagename variable in various places
    <body class="<?echo($pagename);?>">

    so that I can have a css file that automagically tracks 'here' states
    .about a.btn {background:#fff;}

    with
    string[] pagename = System.IO.Path.GetFileName(Page.Request.PhysicalPath).ToLower().Split('.');
    masterBody.Attributes.Add("class", pagename[0]);
    in the master.cs i can get the file names printed as a class name. but then we need to worry about people using the right page name. with the php code above, any random person could make a page, attach the header, and it would automatically have the right css classes.

    worst problem i'm having is basically trying to track down the object model and how the pages communicate to each other...

    doesn't help i've been doing .net for 3 days total now.

    flatlinegraphics on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited July 2009
    Oh boy... I gotta reach down deep into the archives for that one...

    Soulstalkers idea might work and may be the best bet for code-file borne variables.

    If you need to find an actual page control, you can use a method on the asp:content control something along the lines of "findControl()" and then cast it to whatever is appropriate




    This is one of the uglier parts of .NET

    It would be easier if I could see your page code.

    Jasconius on
    this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

    we also talk about other random shit and clown upon each other
  • LegionnairedLegionnaired Registered User regular
    edited July 2009
    So you need to have your child page include a custom css page? So like, reviews get 'review.css?'

    In the master:
    <head>
     <link id="page_specific" rel="stylesheet" type="text/css" href="override.css"/>
    </head>
    

    In the child:
     <script type="text/javascript">
       $('page_specific').href = "gubbins.css";
     </script>
    

    Legionnaired on
  • flatlinegraphicsflatlinegraphics Registered User regular
    edited July 2009
    Hey guys, back from the weekend, and back to the grind.
    Jasconius wrote: »
    Oh boy... I gotta reach down deep into the archives for that one...

    Soulstalkers idea might work and may be the best bet for code-file borne variables.

    If you need to find an actual page control, you can use a method on the asp:content control something along the lines of "findControl()" and then cast it to whatever is appropriate



    This is one of the uglier parts of .NET

    It would be easier if I could see your page code.

    masterpage, stripped of everything but the contentplaceholder
    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="~/ac_sharp.master.cs" Inherits="MasterPage" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <asp:ContentPlaceHolder id="head" runat="server">
        </asp:ContentPlaceHolder>
        <link href="default.css" rel="stylesheet" type="text/css" />
        <!--[if lt IE 7]>
        <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE7.js" type="text/javascript"></script>
        <![endif]-->
    </head>
    <body runat="server" id="masterBody">
      <form id="main" runat="server" class="main">
            <asp:ContentPlaceHolder id="mainContent" runat="server">
            
            </asp:ContentPlaceHolder>
      </form>
    </body>
    </html>
    
    
    CS source for the masterpage
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    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;
    
    
    //
    
    
    
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e){
            //
            //grab the url as a variable, split it at the . and add the page name to the body as a class.
            //Currently, the index page is the only page that this is required. since the rest of the site will be
            //dynamic, we can't count on the url
            //
            string[] pagename = System.IO.Path.GetFileName(Page.Request.PhysicalPath).ToLower().Split('.');
            masterBody.Attributes.Add("class", pagename[0]);
    
    
        }  
    }
    
    
    and a content page
    <%@ Page Language="C#" MasterPageFile="~/ac_sharp.master" Title="Detail Page"%>
    <%@ Register TagName="Designer" TagPrefix="Subnav" Src="~/controls/subnavDesigner.ascx"%>
    
    <asp:Content ID="kors" ContentPlaceHolderID="mainContent" Runat="Server">
        <Subnav:Designer ID="subNavigation" ContentPlaceHolderID="mainContent" Runat="Server" />
        <p>
        blah blah blah
        </p>
    </asp:Content>
    

    and Legionaired, its less costom styles for any given page, and more using the cascade to automagically set 'here' states on buttons, set the backgroundimages for titles and things like that. and having that many extra css files makes maintenance a bit more nightmarish. one masterpage and one css file to rule them all.

    soulstalker:
    Page.GetType() returns the same thing as Page.Request.PhysicalPath. which still requires me to know what the page will be name, rather than declaring what the page is.

    i'd love to be able to (psuedocode!) Page.ContentPlaceHolderId["mainContent"].ID, but that evidently does not work.

    thanks for all the help so far, giving me terms to search from at least.

    flatlinegraphics on
Sign In or Register to comment.