简体   繁体   English

如何将子控件添加到标签控件?

[英]How to add child controls to Label control?

I'm making a WebForms user-control on an ASP.NET webpage. 我正在ASP.NET网页上制作WebForms用户控件。 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. LoggedInLabelVisible属性设置为true,并且标签的<span>标签显示在生成的HTML中,但是标签之间没有任何内容-没有子控件标记,也没有文本。

From LogInControl.ascx : 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 : 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): 从生成的HTML代码中(在LogInButton_Click处理程序之后):

<span id="LogInControl1_LoggedInLabel"></span>

Using the debugger, I confirmed that LoggedInLabel.Visible and LoggedOutLabel.Visible are being assigned true and false , respectively. 使用调试器,我确认分别为LoggedInLabel.VisibleLoggedOutLabel.Visible分配了truefalse I don't understand why the contents of LoggedInLabel don't appear in the HTML code. 我不明白为什么LoggedInLabel的内容没有出现在HTML代码中。 Reloading the page fixes the problem, ie 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! 我真的为此付出了很多,因此,我非常感谢您的帮助!

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. 请记住,Label用作关联输入框(例如文本框或下拉菜单)的文本,因此此处未真正正确使用它。

Try removing the outer label, changing the inner on to a Literal, and then modifying changing the function to something like this: 尝试删除外部标签,将内部标签更改为Literal,然后将功能修改为如下所示:

<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.
        }

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

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