Solved

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

Posted on 2011-03-18
4
254 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

20 Experts available now in Live!

Get 1:1 Help Now