asp:HyperLink is not rendered on browser
-
13-06-2021 - |
题
Hello guys I'm stuck with this weird problem ,asp:HyperLink
control is not being rendered inside browser,it does not even exists in the final HTML sent to the browser.
Home.aspx Asp.net Markup
<div class="LoginBox">
<div id="LoginViewBox" runat="server">
<asp:HyperLink ID="linkLogout" runat="server" OnClick="LogOut"></asp:HyperLink>
</div>
</div>
Home.aspx in the browser
<div class="LoginBox">
<div id="ctl00_HeadHolder_LoginViewBox"> Welcome owaisBhai
</div>
</div>
Home.Aspx.Cs
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoginBoxManager.PopulateLoginBox(ref LoginViewBox, ref linkLogout);
}
}
LoginBoxManager.cs
public static class LoginBoxManager
{
public static void PopulateLoginBox(ref HtmlGenericControl loginViewBox, ref HyperLink linkLogout)
{
string LogInUrl = "Login.aspx";
string WelcomeGuest = String.Format(" Welcome Guest <a href='{0}'>[LogIn]</a>", LogInUrl);
string WelcomeUser = "";
if (HttpContext.Current.Session.Count == 0)
{
//user not authenticated
loginViewBox.InnerHtml = WelcomeGuest;
}
else
{
//user authenticated
WelcomeUser =
String.Format(" Welcome {0} "
,SmartSession<User>.LiveSession.UserName);
loginViewBox.InnerHtml = WelcomeUser;
linkLogout.Text = "[LogOut]";
}
}
}
P.S: I think I have well explained the situation let me know of any details.
解决方案
Your code is replacing the asp:HyperLink with text. The problem line is this line:
loginViewBox.InnerHtml = WelcomeGuest;
By setting the InnerHtml property, you are replacing the HTML that will appear in your LoginViewBox div.
Instead of this approach, i would recommend adding a label into the div. This would give you a final structure of:
<div id="LoginViewBox">
<asp:Label ID="LoginLabel" runat="server"/>
<asp:HyperLink ID="linkLogout" runat="server" OnClick="LogOut"></asp:HyperLink>
</div>
(it's been a while since I've done straight ASP.NET, so there might be more required attributes for the label control).
其他提示
You don't seem to be setting the linkLogout.NavigateUrl
property anywhere. Try setting that.