[英]Unable to render html in ascx control page
Here is my ascx control page code 这是我的ascx控制页面代码
<%@ 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 这是我的C#代码
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> ");
}
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> ");
}
}
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 当我加载页面时,我只会看到以文本形式打印而不是呈现的html代码。任何人都可以指出可能是错误的
You could use a literal instead of a label. 您可以使用文字而不是标签。
See this http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx 看到这个http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx
You can make a div
server accessible by assigning ID
and setting runat="server"
instead of label and set its InnerHTML = menuHtml.ToString(); 您可以通过assigning ID
并设置runat="server"
而不是label来使div
服务器可访问,并设置其InnerHTML = menuHtml.ToString();
<div id="div1" runat="server" ></div>
div1.InnerHTML = menuHtml.ToString();
change the Label 更改标签
<asp:Label ID="lblMenu" runat="server"></asp:Label>
to Literal as 以文字为
<asp:Literal ID="lblMenu" runat="server" EnableViewState="false"></asp:Literal>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.