c# 如何动态添加html标签,并给各个标签设置属性,有的属性不能重复,有的属性有限制,你就知道的。

c# 如何动态添加html标签,并给各个标签设置属性,有的属性不能重复,有的属性有限制,你就知道的。,第1张

.aspx.cs文件需要

using System.Web.UI.WebControls

using System.Web.UI.HtmlControls添加这两个引用;

这是例句代码,楼主可研究一番,如有不明,可Hi我,留言就可,

((HtmlInputText)this.Form.FindControl("txtTitle")).Value = dr["Title"].ToString()

((HtmlInputCheckBox)this.Form.FindControl("cbTop")).Checked = string.IsNullOrEmpty(dr["IsTop"].ToString()) ? false : bool.Parse(dr["IsTop"].ToString())

((CKEditorControl)this.Form.FindControl("txtNContent")).Text = dr["NContent"].ToString().Replace("<", "<").Replace(">", ">")

一、使用javascript 模板引擎

用javascript预编译模版,就是动态修改模板文件使之成为一个可用的静态HTML文件。 我平时会使用artTemplate,性能很好而且易上手。

编写模板

使用一个type="text/html"的script标签存放模板:

<script id="test" type="text/html">

<h1>{{title}}</h1>

<ul>

{{each list as value i}}

<li>索引 {{i + 1}} :{{value}}</li>

{{/each}}

</ul>

</script>

渲染模板

var data = {

title: '标签',

list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']

}

var html = template('test', data)

document.getElementById('content').innerHTML = html

二、使用CoffeeScript

CoffeeScript支持类似于Python的跨行字符串,这样很轻易的就能保持HTML结构的可读性,而不需要使用“+”或者采用拼数组的形式。

str="""

<div class="dialog">

<div class="title">

<img src="close.gif" alt="关闭" />关闭

</div>

<div class="content">

<img src="delete.jpg" alt="" />

</div>

<div class="bottom">

<input id="Button2" type="button" value="确定" class="btn"/>

<input id="Button3" type="button" value="取消" class="btn"/>

</div>

</div>

"""


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/bake/11909652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存