简体   繁体   中英

Solution for use <% … %> where runat=“server” (or vice versa) in asp.net

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 , eg:

<!--log in | only show when log out. -->
    <asp:Panel CssClass="user" id="logOut" runat="server">
        <asp:HyperLink NavigateUrl="~/gebruikers/aanmelden.aspx" CssClass="click" id="logIn" Text="Log in" runat="server" />
        <asp:HyperLink NavigateUrl="~/gebruikers/registreren.aspx" CssClass="click" id="regist" style="left:100px" Text="Regist" runat="server"/>
    </asp:Panel>

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, eg:

if (Request.Cookies["user"] != null) // log in
{
    logOut.Visible = false; // 2
}
else // log out 
{
    logIn.Visible = false; // 2
    messages.Visible = false;
}

There are a few different varieties of ASP.net inline tags. Please see the full list here: https://support.microsoft.com/en-us/kb/976112

Not all of them support placement inside the attributes of a server-side control's tag. The <%# ... %> data-binding expression inline format would allow you to do that, and I think the older <% ... %> format also. The <%= ... %> inline tag will definitely not work inside the server-side control tag because the whole expression is directly compiled instead of displaying the content as an attribute value.

If your main goal is controlling visibility of a server-side control, then you should just be able to set control.Visible = false; in your code-behind. If you'd like to control visibility of a non-server-side control (or a block of controls), then the <asp:Panel> server-side control might be your best route. ASP.net has tried to move away from the excessive inlining approach of the old ASP.

I used to get errors similar to the one you specified. Because the ResolveUrl uses "" , avoid using that for the HREF attribute too as it might break the code. Try the below code:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM