i got “Object reference not set to an instance of an object”. in my master page code behind. how to solve this?
Site.Master.Cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["IsLogin"] == "1")
{
((HyperLink)HeadLoginView.FindControl("Login")).Visible = false;
((HyperLink)HeadLoginView.FindControl("Logout")).Visible = true;
}
else
{
((HyperLink)HeadLoginView.FindControl("Login")).Visible = true;
((HyperLink)HeadLoginView.FindControl("Logout")).Visible = false;
}
}
Site.master
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ <a href="~/Default.aspx" id="HeadLoginStatus" runat="server">Log In</a> ]
[ <a href="~/Logout.aspx" id="HeadLoginStass" runat="server">Log Out</a> ]
</AnonymousTemplate>
The way you try to locate your controls is off. The
HeadLoginView.FindControl("Login"))comes back as null and your further attempt to setVisibleon it blows upYou should type your control id exactly as it shows up in the page definition `HeadLoginStatus’ for the login button I assume