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