空参考Exc。智能感知检测到的标签上?
好吧,我对 ASP.NET 和 MasterPage 概念还很陌生,但有一个我无法弄清楚的错误。
这是我的 default.aspx 的一部分:
<asp:Content ID="ContentLoginContent" ContentPlaceHolderID="LoginContentPlaceHolder" runat="server">
<div id="ContentLoginDiv">
You've got <asp:Label ID="MemberCreditLabel" runat="server" Text="0"></asp:Label> credits.
</div>
这是我的 default.aspx.cs 的相关部分:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
if (User.IsInRole("Authenticated"))
{
MemberCreditLabel.Text = "hello ";
}
}
}
我在 MemberCreditLabel 上收到 Nullref 异常。它可以通过 IntelliSense 检测到。我认为问题可能是 ContentPlaceHolder“ContentLoginContent”仅在登录时显示。这是我的 MasterPage 的一部分:
<asp:LoginView ID="MemberLoginView" runat="server">
<AnonymousTemplate>
<asp:Login ID="LogInBox" runat="server" Height="137px" style="margin-left: 0px"
Width="16px">
</asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <asp:LoginName ID="MemberLoginName" runat="server" /> !
<asp:LoginStatus ID="MemberLoginStatus" runat="server" />
<asp:ContentPlaceHolder ID="LoginContentPlaceHolder" runat="server">
//Is this the problem?
</asp:ContentPlaceHolder>
</LoggedInTemplate>
</asp:LoginView>
我想要做的是显示存储在数据库中的信用金额。用于检索我想要的数据的功能有效。我获取当前登录用户的用户名,并希望获取与该用户关联的信用金额。但是这个奇怪的标签错误让我完全停止了。这可能与我还没有偶然发现的 MasterPages 概念有关。有什么想法吗?
Okay so I'm quite new to ASP.NET and the MasterPage concept and there's an error I just can't figure out.
This is a part of my default.aspx:
<asp:Content ID="ContentLoginContent" ContentPlaceHolderID="LoginContentPlaceHolder" runat="server">
<div id="ContentLoginDiv">
You've got <asp:Label ID="MemberCreditLabel" runat="server" Text="0"></asp:Label> credits.
</div>
This is the relevant part of my default.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
if (User.IsInRole("Authenticated"))
{
MemberCreditLabel.Text = "hello ";
}
}
}
I get a Nullref exception on MemberCreditLabel. It gets detected with intelliSense. I think the problem could be that the ContentPlaceHolder "ContentLoginContent" only shows when logged in. This is a part of my MasterPage:
<asp:LoginView ID="MemberLoginView" runat="server">
<AnonymousTemplate>
<asp:Login ID="LogInBox" runat="server" Height="137px" style="margin-left: 0px"
Width="16px">
</asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <asp:LoginName ID="MemberLoginName" runat="server" /> !
<asp:LoginStatus ID="MemberLoginStatus" runat="server" />
<asp:ContentPlaceHolder ID="LoginContentPlaceHolder" runat="server">
//Is this the problem?
</asp:ContentPlaceHolder>
</LoggedInTemplate>
</asp:LoginView>
What I want to do is to show a credit amount stored in a database. The function for retreiving the data I want works. I take the user name of the currently logged in user and want to get the credit amount associated with the user. But this strange error with the label halts me completely.. It's probably got to do with a concept of MasterPages that I haven't stumbled across yet. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然这是 设计:
但是,使用递归 FindControl,我无法实际获取 LoggedInTemplate 内的控件 - 实际上在页面的标记中,ReSharper 抱怨它无法解析符号“LoginContentPlaceHolder” - 即它无法解析无法在母版页上正确找到内容占位符。
有什么方法可以在所有经过身份验证的页面上显示积分吗?
或者,您可以将 LoginView 与仅包含信用计数的 LoggedInTemplate 包装到用户控件中,并将其放入内容占位符内。
Apparently this is by design:
However, using a recursive FindControl, I wasn't able to actually get hold of the control inside the LoggedInTemplate - indeed in the markup of the Page, ReSharper was complaining that it couldn't resolve the symbol "LoginContentPlaceHolder" - i.e. it couldn't find the content placeholder correctly on the MasterPage.
Is there any way you could display the credits on all authenticated pages?
Or, you could wrap the LoginView with just a LoggedInTemplate containing the credit count into a usercontrol, and drop that inside the content placeholder.