Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-18
4
Medium Priority
?
260 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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