从asp.net后面的代码编写HTML标记 [英] Write Html Markup from code behind in asp.net

查看:36
本文介绍了从asp.net后面的代码编写HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果标题令人困惑,建议将其更改

从背后的代码编写自己的HTML的最佳方法是什么?

What is the Best way to write my own HTML from code behind?

我目前正在使用此

<asp:Literal ID="ltr" runat="server"></asp:Literal>

以及后面的代码:

ltr.Text = "<p class=\"specific-class\"></p>";

这样做是权利吗?

有更好的方法吗?

推荐答案

您可以这样做,但请不要这样做.为了您的开发人员过去,现在和将来,请从设计中分离代码.现在,如果您必须在后面的代码中编写标记,那无疑是一种实现标记的方法.

You can do it like that, but please don't. For the sake of your fellow developers in the past, present and the future, seperate your code from your design. Now, if you must write markup in code behind, that is most certainly a way to do it.

但是,如果您要添加的是带有特定类的文字/跨度/文本框,则可以执行以下操作:

However, if all you want is to add a literal / span / textbox with a certain class you can do something like this instead:

(假设您有一个带有 runat ="server" 的面板,名为"myPanel")

( Given that you have a panel with runat="server" that is named "myPanel" )

myPanel.Controls.Add(new Label
                     { Text = "Hello!", CssClass = "specific-class" });

这篇关于从asp.net后面的代码编写HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆