简体   繁体   English

使用<%…%>的解决方案,其中asp.net中的runat =“ server”(反之亦然)

[英]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. 用户单击带有他的头像a标签时,他必须重定向到另一个页面。 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. 使用此C#代码,我可以根据登录或注销来放置一些不可见的标签。

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. 有关代码的其他信息:如果您登录,我将放置一个带有用户ID的cookie。 If the cookie is not null, the user is login, other ways not. 如果cookie不为null,则表明用户正在登录,而其他方式则不是。 If you are login, it place the a -tag with id logout unvisible. 如果您是登录用户,则它将ID logout a -tag置于不可见状态。

My problem 我的问题

Now this code will give a NullReferenceException on line two. 现在,此代码将在第二行给出NullReferenceException

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: 如果我把runat="server"a标签都有效,它给我这个:

Server Labels should not contain <% ... %> -constructs. 服务器标签不应包含<% ... %> -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. 在上面的代码a -tag上添加了<% ... %> -constructor,以获取正确的URL进入正确的页面。

This is my problem. 这是我的问题。 You can not add a <% ... %> -constructor where runat="server" stand. 您不能在runat="server"所在的位置添加<% ... %> -constructor。 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. 例如,从messages/inbox.aspx转到user/profile.aspx必须很重要。

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: 除了使用普通a -tags之外,还可以使用PanelHyperlinks之类的WebForm控件,例如:

<!--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: 这可能会减少对生成的HTML的控制量(因此,您必须检查HTML是否对您有好处),但可以使您更轻松地访问后面的Code中的控件,例如:

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. ASP.net内联标签有几种不同的变体。 Please see the full list here: https://support.microsoft.com/en-us/kb/976112 请在此处查看完整列表: 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; 如果您的主要目标是控制服务器端控件的可见性,那么您应该就可以设置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:Panel>服务器端控件可能是您的最佳选择。 ASP.net has tried to move away from the excessive inlining approach of the old ASP. ASP.net试图摆脱旧的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. 由于ResolveUrl使用"" ,因此也请避免将其用于HREF属性,因为它可能会破坏代码。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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