Solved

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

Posted on 2011-03-18
4
253 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
  • 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now