2

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.

DayTimeCoder
  • 4,294
  • 5
  • 38
  • 61

2 Answers2

2

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

Jeff Siver
  • 7,434
  • 30
  • 32
  • Wow Jeff Siver your answer was a SilverBullet it worked and I'm thinking now what a stupid mistake I made :) – DayTimeCoder Jun 09 '12 at 20:30
  • `` doesn't have an innerHtml attribute, but `
    ` does? That's interesting. Doesn't the `
    ` need a `runat="server"` attribute for it to be referenced in the code-behind?
    – MrBoJangles Jan 22 '14 at 21:41
  • If I remember correctly, my recommendation meant removing the code behind that updated LoginViewBox which is why I didn't have the runat="server" tag. And the asp:Panel control doesn't have an innerHtml attribute because it has a Controls collection which provides better support for these types of operations. – Jeff Siver Jan 24 '14 at 18:15
1

You don't seem to be setting the linkLogout.NavigateUrl property anywhere. Try setting that.

Nickoli Roussakov
  • 3,434
  • 16
  • 22