Link to home
Start Free TrialLog in
Avatar of Tim
Tim

asked on

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

Avatar of plusone3055
plusone3055
Flag of United States of America image

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 ?
Avatar of iteealro
iteealro

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.
Avatar of Tim

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Fareed Ali Khan
Fareed Ali Khan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial