My idea
When the user click on the a
tag with his avatar, he must redirect to another page. I do this with the code by number one (see below).
<div>
<!--show new messages | only show when log in. -->
<a href="<%=ResolveUrl("~/messages/inbox.aspx") %>" class="click headeritem" id="messages">
<img src="<%=ResolveUrl("~/images/message.png") %>" alt="new messages" id="messages" />
<asp:Label class="number" id="lblNewMessages" runat="server">1</asp:Label>
</a>
<!--log in | only show when log out. -->
<div class="user" id="logOut" runat="server">
<a href="<%=ResolveUrl("~/gebruikers/aanmelden.aspx") %>" class="click" id="logIn">Log in</a>
<a href="<%=ResolveUrl("~/gebruikers/registreren.aspx") %>" class="click" id="regist" style="left:100px">Regist</a>
</div>
<!--go to the profile of the user | only show when log in. -->
<!--1-->
<a class="click user" id="logIn" href="<%=ResolveUrl("~/gebruiker.aspx") %>">
<img id="picture" src="<%=ResolveUrl("~/afbeeldingen/person-male.png") %>" alt="user" />
<asp:Label runat="server" id="points" class="points">10</asp:Label>
</a>
</div>
With this C# code I place some tags invisible dependent on log in or out.
if (Request.Cookies["user"] != null) // log in
{
FindControl("logOut").Visible = false; // 2
}
else // log out
{
FindControl("logIn").Visible = false; // 2
FindControl("messages").Visible = false;
}
Extra information about the code: If you are login, I place a cookie with the user's id. If the cookie is not null, the user is login, other ways not. If you are login, it place the a
-tag with id logout
unvisible.
My problem
Now this code will give a NullReferenceException
on line two.
Additional information: Object reference not set to an instance of an object.
If I place runat="server"
to the a
-tags, it give me this:
Server Labels should not contain
<% ... %>
-constructs.
There is an <% ... %>
-constructor added on the a
-tag in the code above for get the correct url to go to the correct page.
This is my problem. You can not add a <% ... %>
-constructor where runat="server"
stand. How can you do it on a correct way?
Other information
Maybe also important to know is that my project has sub directories. It must be important to go from messages/inbox.aspx
to user/profile.aspx
for eg.
All this code above is added to a master page that I use for all the pages.
Can anyone help me? Thanks and sorry for my poor English.
Instead of using plain
a
-tags, you could use WebForm-controls like Panels or Hyperlinks, e.g.:This might reduce the amount of control you have over the generated HTML (so you'd have to check whether the HTML is good for you), but would enable you to access the controls in Code behind more easily, e.g.: