iJade
iJade

Reputation: 23811

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

Upvotes: 0

Views: 2640

Answers (3)

Adil
Adil

Reputation: 148180

You can make a div server accessible by assigning ID and setting runat="server" instead of label and set its InnerHTML = menuHtml.ToString();

<div id="div1" runat="server" ></div>

div1.InnerHTML = menuHtml.ToString();

Upvotes: 3

anon
anon

Reputation:

You could use a literal instead of a label.

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

Upvotes: 6

Aristos
Aristos

Reputation: 66649

change the Label

<asp:Label ID="lblMenu" runat="server"></asp:Label>

to Literal as

<asp:Literal ID="lblMenu" runat="server" EnableViewState="false"></asp:Literal>

Upvotes: 2

Related Questions