?
Solved

Need list of controls on master page and/or page.

Posted on 2011-03-18
4
Medium Priority
?
259 Views
Last Modified: 2012-05-11
This might seem like a very typically request but I'm not trying to find a control located on the master page from a content page which is making this very hard to figure out.

When I create a user control I also create an external style sheet. This keeps everything nice and neat. I started off putting the htmllink reference in the master page as a static entry but as I create more and more controls, this list is getting longer and being more of a mess. I came up with this brilliant idea of having the master page create a list of user controls and checking to see if a style sheet exists for any of them. If a stylesheet is found then I want it to dynamically add the stylesheet.

I've got the coding for dynamically adding the style sheet. I have the coding for checking to see if the file exists. I keep trying to use page.controls or master.controls to get the list of controls and but I'm not having much luck. I just need to build a list of the control id's that exists on the page. Perferrably on the master page and if possibly on the content page.

This can be done with seperate functions if the master page and the content page needs to be seperate.

I've included some sample code that does not work but that I think is fairly close.


Private Sub UserControlStyles()
        Dim Ctrl As Control
        Dim UserControlHtmlLink As New HtmlLink

        For Each Ctrl In Master.Controls
          If TypeOf Ctrl Is UserControl Then
            If clsFileSystem.fnFileExists(Server.MapPath("/Styles/Controls/" & Ctrl.ID.ToString & ".css")) Then
              UserControlHtmlLink.Href = "/Styles/Controls/" & Ctrl.ID & ".css"
              UserControlHtmlLink.Attributes.Add("rel", "stylesheet")
              UserControlHtmlLink.Attributes.Add("type", "text/css")
              UserControlHtmlLink.Attributes.Add("media", "all")
              Page.Header.Controls.Add(UserControlHtmlLink)
            End If
          End If
        Next
    End Sub

Open in new window

0
Comment
Question by:CubeRoot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:foobarr
ID: 35168798
Does your master page have any other placeholder or perhaps panels

while you debug your code

for each ctrl in master.controls

master.controls.count should return a value, see what value that is.
also ctrl.id should display the id of the first control.  Locate that control in your source code and see if there are other levels of controls


you may have to do something messy like

for each ctrl in master.controls  
   for each subctrl in ctrl.controls
         for each subsubctrl in subctrl.controls

before you even reach the controls you are looking for
0
 
LVL 3

Author Comment

by:CubeRoot
ID: 35168881
When I try to reference master.controls.Count I get Object reference not set to an instance of an object.

I'll try playing around with the subctrl coding but I'm not sure how to get around the object reference issue.

<%@ Master Language="VB" AutoEventWireup="false" CodeFile="1Column.Master.vb" Inherits="Site"       %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
    <form runat="server" class="frmMain" id="frmMain">
    <asp:ScriptManager ID="ScriptManager" runat="server" />
    
    <div class="page">
        
        <uc:SignIn      id="ucSignIn"       runat="server" />
        <uc:Header      id="ucHeader"       runat="server" />

        <div class="main">
            <uc:Navigation          id="ucNavigation"       runat="server" />
            <uc:ArticleEmbedded     id="ucArticleEmbedded"  runat="server" />
            <asp:ContentPlaceHolder id="MainContent"        runat="server" />
        </div>
        <div class="clear"><!-- --></div>
    </div>
    
    <div class="footer" runat="server" id="footer"><!-- --></div>

    </form>
</body>
</html>

Open in new window

0
 
LVL 7

Accepted Solution

by:
foobarr earned 2000 total points
ID: 35168928
did you try

for each ctrl as control in frmMain.controls

next


we are probably searching the wrong area, just means it can't find the control on the page
0
 
LVL 3

Author Comment

by:CubeRoot
ID: 36010724
       ' Dynamically load style sheet for user controls
        For Each Ctrl As Control In frmMain.Controls
            If TypeOf Ctrl Is UserControl Then
                If clsMiscellaneous.FileExists(Server.MapPath("/Styles/Controls/" & Ctrl.ID & ".css")) Then
                    Dim UserControlHtmlLink As New HtmlLink
                    UserControlHtmlLink.Href = "/Styles/Controls/" & Ctrl.ID.ToString & ".css"
                    UserControlHtmlLink.Attributes.Add("rel", "stylesheet")
                    UserControlHtmlLink.Attributes.Add("type", "text/css")
                    UserControlHtmlLink.Attributes.Add("media", "all")
                    Page.Header.Controls.Add(UserControlHtmlLink)
                    UserControlHtmlLink.Dispose()
                End If
            End If
        Next

That's my final code I ended up using. Thanks for the help and sorry for the late response.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question