Solved

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

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

732 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