Reputation: 161
I'm making a WebForms user-control on an ASP.NET webpage. It should change its contents depending on the user being logged in or out.
The problem occurs on a post-back after the login form's submit button is clicked. The Visible
property of LoggedInLabel
is set to true, and the label's <span>
tags appear in the generated HTML, but there's nothing between the tags - no child controls markup and no text.
From LogInControl.ascx
:
<asp:Label ID="LoggedInLabel" runat="server">
Logged in as: <asp:Label ID="UserLoggedInName" runat="server" />
- <asp:HyperLink ID="LogOutLink" runat="server">Log out</asp:HyperLink>
</asp:Label>
From LogInControl.ascx.cs
:
protected void LogInButton_Click(object sender, EventArgs e)
{
//(...)
bool isUserLoggedIn = AuthHelper.isUserLoggedIn(Session);
LoggedInLabel.Visible = isUserLoggedIn;
LoggedOutLabel.Visible = !isUserLoggedIn;
if (isUserLoggedIn)
{
UserLoggedInName.Text = AuthHelper.getUserLoggedIn(Session).ToString();
}
//(...)
}
From generated HTML code (after LogInButton_Click
handler was called):
<span id="LogInControl1_LoggedInLabel"></span>
Using the debugger, I confirmed that LoggedInLabel.Visible
and LoggedOutLabel.Visible
are being assigned true
and false
, respectively. I don't understand why the contents of LoggedInLabel
don't appear in the HTML code. Reloading the page fixes the problem, i.e. the contents change to this:
<span id="LogInControl1_LoggedInLabel">
Logged in as: <span id="LogInControl1_UserLoggedInName">login (Fname Lname)</span>
- <a id="LogInControl1_LogOutLink">Log out</a></span>
I'm really pulling my hair out over this, so I'd be really grateful for some help!
Upvotes: 1
Views: 701
Reputation: 1489
without being able to see the rest of the code for the page, it's tough to tell, but I think it 'be because you have nested labels and the outer one doesn't refresh until the page does.
Remember that the Label is used as text for an associated input box such as a textbox or dropdown, so it's not really being used properly here.
Try removing the outer label, changing the inner on to a Literal, and then modifying changing the function to something like this:
<asp:Panel ID="LoggedInPanel" runat="server" visible="False">
<asp:Literal ID="UserLoggedInName" runat="server" />
- <asp:HyperLink ID="LogOutLink" runat="server">Log out</asp:HyperLink>
</asp:Label>
and
LoggedInPanel.Visible = isUserLoggedIn;
if (isUserLoggedIn)
{
UserLoggedInName.Text = String.Format("Logged in as: {0}",AuthHelper.getUserLoggedIn(Session).ToString());
// Set the logout URL here.
}
Upvotes: 1