Cannot access master page control from Page-Load()

Object reference not set to an instance of an object.

This worked before I set it up as a master page now it doesn't work. Issue is on Line 23 of the code snippet trying to access the DIV.

Thanks


<%@ Master Language="C#" AutoEventWireup="true" Debug="true" %>
<%@ Assembly Name="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head1" runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="index.css" type="text/css"/>
        
        <script runat="server">
        
        protected void Page_Load(Object sender, EventArgs e)
        {
            String username = HttpContext.Current.User.Identity.Name;
            String sub = username.Substring(0, 4);
            username = username.Replace(sub, "");
            System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://DC=nam,DC=nsroot,DC=net");
            System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
            mySearcher.Filter = "(&(objectClass=user)(samAccountName=" + username + "))";
            System.DirectoryServices.SearchResult searchResult = mySearcher.FindOne();
            adName.InnerText = "Logged in User: " + username + " - " + searchResult.Properties["displayName"][0].ToString();
        }
        
        </script>
    </asp:ContentPlaceHolder>
</head>

<body>
    <asp:ContentPlaceHolder id="main" runat="server">
    <div id="adName" style="text-align: center" runat="server">
 
    </div>
 	</asp:ContentPlaceHolder>
</body>
</html>

Open in new window

tr57285Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
object not set to instance of object is looking for something that is not there.
have you performed a step-thru (F10)
or put in a StackTrace expection to see what is coming up blank ?
0
iteealroCommented:
Maybe it is the script, which is in the head, is trying to call on a control that is in the body, and it has not rendered yet when the head loads? I am not entirely sure that would make a difference.
0
tr57285Author Commented:
I will try the stack trace but as I said nothing changed other than making this a master page. Previously this was a normal aspx page and it worked fine.

Not sure why it isn't working now.
0
Fareed Ali KhanCommented:
Why you have Head in asp:ContentPlaceHolder? It is used to be for some page contents which will use the Master Page. Also ContentPlaceHolder is for HTML controls not for head. Head commonly contain only invisible information. Or for Meta tags.

You need to place the asp:ContentPlaceHolder in the body tags.


<%@ Master Language="C#" AutoEventWireup="true" Debug="true" %>
<%@ Assembly Name="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="index.css" type="text/css"/>
       
        <script runat="server">
       
        protected void Page_Load(Object sender, EventArgs e)
        {
            String username = HttpContext.Current.User.Identity.Name;
            String sub = username.Substring(0, 4);
            username = username.Replace(sub, "");
            System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://DC=nam,DC=nsroot,DC=net");
            System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
            mySearcher.Filter = "(&(objectClass=user)(samAccountName=" + username + "))";
            System.DirectoryServices.SearchResult searchResult = mySearcher.FindOne();
            adName.InnerText = "Logged in User: " + username + " - " + searchResult.Properties["displayName"][0].ToString();
        }
       
        </script>
</head>

<body>

    <asp:ContentPlaceHolder id="head1" runat="server">

    </asp:ContentPlaceHolder>

    <asp:ContentPlaceHolder id="main" runat="server">
    <div id="adName" style="text-align: center" runat="server">
 
    </div>
       </asp:ContentPlaceHolder>
</body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.