c# - 如何在 asp.net 中将字符串呈现为 html?
问题描述
将表中的输入字段字符串转换为输入字段?
<%= HttpUtility.HtmlDecode((string)objRow["Post"]) %>
在视图结束
<%= HttpUtility.HtmlDecode(GetUsersList())%>
Cs 文件中的代码
foreach (DataRow dtRow in ds.Tables[0].Rows)
{
string userpk = Convert.ToString(dtRow["user_pk"]);
string usertypecd = Convert.ToString(dtRow["user_type_cd"]);
string firstname = Convert.ToString(dtRow["first_name"]);
string lastname = Convert.ToString(dtRow["last_name"]);
string active = Convert.ToString(dtRow["active_ind"]);
if (active == "true")
{
active = "< input type = 'checkbox' class='editor-active' disabled='disabled' checked='checked'>";
}
else {
active = "< input type = 'checkbox' class='editor-active' disabled='disabled'>";
}
string phoneno = Convert.ToString(dtRow["phone_no"]);
string phone_ext = Convert.ToString(dtRow["phone_ext"]);
string email = Convert.ToString(dtRow["email"]);
content += "<tr><td>"+ usertypecd+ "</td><td>" + firstname + "</td><td>" + lastname + "</td><td>" + active + "</td><td>" + phoneno + "</td><td>" + phone_ext + "</td><td>" + email + "</td><td></td></tr>";
}
return content;
结果图像:
解决方案
如果您的意思是 Asp-WebForms,您可以使用属性System.Web.Label
中的 html 文本创建一个。Text
如果您将控件添加到页面,这将呈现为 HTML。
或者,您可以将文本转储到受保护/公共属性中,然后<%= YourPropertyNameHere %>
将文本原样转储到页面中。
我个人建议您构建您的 Web 控件而不是字符串操作。例如,创建一个System.Web.Panel
,然后根据需要将您的 html 控件添加到其中。
var pnl = new Panel();
var cb = new CheckBox();
cb.Enabled = (active == "true");
cb.Checked = cb.Enabled;
cb.CssClass = "editor-active";
pnl.Controls.Add(cb);
SomePanelInFrontCode.Controls.Add(pnl);
推荐阅读
- android - RecyclerView 行为:特定行自定义在其他行上重复(Kotlin)
- c# - 在我的 MVC 应用程序中添加迁移时,什么可能导致我的空对象异常?
- maven - Spring Tool Suite - 3.9.5 - 中央 maven 回购 - 重新索引
- google-chrome - 更改 Chromecast 发件人上的应用 ID
- c# - Unity 预制件作为检查器中的对象要求附加对象
- html - 检测哪一部分被点击?
- ruby - Ruby 和/或 python:将 telnetdata 写入文件
- r - 在 R Plotly 中更改图例标题
- javascript - 从具有多个元素的变量中获取一个元素
- windows-installer - 当前安装过程 installshield 2011 的环境变量