Search code examples
c#asp.nethtmlascx

Unable to render html in ascx control page


Here is my ascx control page code

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Menu.ascx.cs" Inherits="CrossQueue.Web.Menu" %>
<asp:Label ID="lblMenu" runat="server"></asp:Label>

Here is my c# code

private void CreateMenu()
    {
        StringBuilder menuHtml = new StringBuilder();   
        int profileId = 0;
        MenuBL menuManager = new MenuBL();
        DataTable dtMenu = null;
        if (Session["USR_ID"] != null)
        {
            profileId = Convert.ToInt32(Session["USR_PROFILE"]);
            dtMenu = menuManager.GetAllMenuItemsForProfile(profileId);
            if (dtMenu != null && dtMenu.Rows.Count > 0)
            {
                menuHtml.Append("<table id='tblMenu' cellpadding='0' cellspacing='0' width='939' border='0' align='center'>");
                menuHtml.Append("<tr>");
                menuHtml.Append("<td align='left'>");
                menuHtml.Append(Convert.ToString(Session["USR_USERNAME"]));
                menuHtml.Append("</td>");

                menuHtml.Append("<td width='739' valign='middle' align='right' style='height: 30px;'>");
                foreach (DataRow dr in dtMenu.Rows)
                {
                    if (dr["MenuName"].ToString() == "Profile")
                    {
                        menuHtml.Append("<img src='/images/home_icon.jpg' width='25' height='25' align='middle' /><a href='AllProfile.aspx>Profile</a>&nbsp;");
                    }
                    else if (dr["MenuName"].ToString() == "User")
                    {
                        menuHtml.Append("<img src='/images/home_icon.jpg' width='25' height='25' align='middle' /><a href='AllUsers.aspx>User</a>&nbsp;");
                    }
                }
                menuHtml.Append("</td>");
                menuHtml.Append("</tr>");
                menuHtml.Append("</table>");
            }
            lblMenu.Text = menuHtml.ToString();
        }
    }

When i load the page i only see a html code printed as text and not rendering.Can any one point out what may be wrong


Solution

  • You could use a literal instead of a label.

    See this http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx