asp.nethtmlvisual-studio-2010domasp.net-webcontrol

asp:HyperLink is not rendered on browser


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">&nbsp;Welcome&nbsp;owaisBhai&nbsp;
  </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("&nbsp;Welcome Guest&nbsp;<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("&nbsp;Welcome&nbsp; {0}&nbsp;"
                                ,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.


Solution

  • 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).