空参考Exc。智能感知检测到的标签上?

发布于 2024-08-19 16:09:39 字数 1825 浏览 5 评论 0原文

好吧,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Bonjour°[大白 2024-08-26 16:09:39

显然这是 设计

这是设计使然。内容控件替换模板内的 contentplaceholder 控件的内容。因此,文本框实际上在模板中被实例化,因此需要使用 FindControl 进行查找 - 直接访问将不可用。

谢谢

网络平台和工具团队。

但是,使用递归 FindControl,我无法实际获取 LoggedInTemplate 内的控件 - 实际上在页面的标记中,ReSharper 抱怨它无法解析符号“LoginContentPlaceHolder” - 即它无法解析无法在母版页上正确找到内容占位符。

有什么方法可以在所有经过身份验证的页面上显示积分吗?

或者,您可以将 LoginView 与仅包含信用计数的 LoggedInTemplate 包装到用户控件中,并将其放入内容占位符内。

Apparently this is by design:

This is by design. The content control replaces the content of the contentplaceholder control which is inside a template. Thus the textbox actually gets instatiated in a template and thus needs to be looked up using FindControl - direct access will not be available.

Thanks,

The WebPlatform and Tools Team.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文