views:

26

answers:

1

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?

A: 

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.

Zhaph - Ben Duguid
My solution was to just keep the label empty when user wasn't logged in. When logged in I fill the label under Page_Load. Don't know if this is the intended way.. there ought to be some best practice for this.
Phil