通过一个小例子讲下动态获取li标签的数据,前台页面原有样式:
<div class="flone">
<ul class="fltwo">
<li class="fole" id="dk1"><a><div class="liuliang">20M</div><div class="jiage">10元</div></a></li>
<li class="foce" id="dk2"><a><div class="liuliang">50M</div><div class="jiage">30元</div></a></li>
<li class="fori" id="dk3"><a><div class="liuliang">100M</div><div class="jiage">50元</div></a></li>
<li class="fole" id="dk4"><a><div class="liuliang">200M</div><div class="jiage">70元</div></a></li>
<li class="foce" id="dk5"><a><div class="liuliang">300M</div><div class="jiage">100元</div></a></li>
<li class="fori" id="dk6"><a><div class="liuliang">500M</div><div class="jiage">150元</div></a></li>
<li class="fole" id="dk7"><a><div class="liuliang">1G</div><div class="jiage">200元</div></a></li>
</ul>
</div>
如上代码所示,现在的需求是把li里的数据动态读取出来,代码修改如下:
<form id="form1" runat="server">
<div class="flone">
<ul class="fltwo">
<%=updateCSS%>
</ul>
</div>
</form>
说明:<%=updateCSS%>updateCSS是后台的变量,在后台要给updateCSS赋值,里面的值就是你自己(从数据库或者接口)获取到的
string url2 = "~/meallist";//获取数据的链接 string ss = HttpHelper.HttpPostResponse(url2, pan, , null, null);//接收到的数据 string[] strArray = ss.Split(new String[] { "##" }, StringSplitOptions.RemoveEmptyEntries); string temp = "<li class=\"{0}\"><a><div class=\"liuliang\">{1}</div><div class=\"jiage\">{2}元</div></a><input type=\"hidden\" name=\"price\" value=\"{2}\" /><input type=\"hidden\" name=\"MEALID\" value=\"{3}\" /></li>";
StringBuilder builder = new StringBuilder();
decimal price;
for (int i=;i< strArray.Length;i++)
{
string[] items = strArray[i].Split('#');
if (i == || i == || i == )
{
price = Math.Round(decimal.Parse(items[]),);
builder.AppendFormat(temp, "foce", items[], price, items[]);
}
else
{
price = Math.Round(decimal.Parse(items[]),);
builder.AppendFormat(temp, "fole", items[], price, items[]);
} }
updateCSS = builder.ToString();
也可以用其他的方式,repeater控件或者datalist控件等等