欢迎加入站点基地设计开发交流群:30720409
详细内容
DataList动态添加行的方法
发布日期:2011-01-08     点击:3889     字体:[ ]

这里介绍DataList动态添加行的办法,思路:

将原DataList行进行拆分,中间添加自己要添加的数据行,然后和原数据行进行拼接。

可为DataList添加ItemDataBound事件,下面这个例子中DataList每行显示两条数据,如果你的每行显示多条,则应添加相应的TD标记,例如:

 

 //行数据处理,随机生成数据

        protected void dl_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //生成随机数
            Random rnd = new Random();
            int n = rnd.Next(0,6);
            if (e.Item.ItemIndex == n)
            {
                StringBuilder txt = new StringBuilder();

                txt.Append("<td align='center'>");
                txt.Append("<div class='pngPic'>");
                txt.Append(ZDBASE.WebUI.message.getGoogleTemplateAd());
                txt.Append("</div>");

                txt.Append("<div class='pngInfo'>");
                txt.Append("<table border='0' cellpadding='0' cellspacing='0'  width='100%' align='left'>");
                txt.Append("     <tr>");
                txt.Append("     <td width='5px' align='center' valign='middle'><img src='../images/png.gif' width='4' height='7' /></td>");
                txt.Append("     <td align='left'>&nbsp;&nbsp;<a href='pngDetail.aspx?sType=1106&sClass=1' class='link01'  target='_blank'>网站常用整站设计模板</a></td>");
                txt.Append("     </tr>");
                txt.Append("     </table>");
                txt.Append(" </div>");

                txt.Append("</td></tr>");
               
                txt.Append("<tr><td align='center'>");
                                           
                //添加百度广告

                txt.Append("<div class='pngPic'>");
                txt.Append(ZDBASE.WebUI.message.getBaiduTemplateAd());

                txt.Append("</div>");
                txt.Append("<div class='pngInfo'>");
                txt.Append("<table border='0' cellpadding='0' cellspacing='0'  width='100%' align='left'>");
                txt.Append("<tr>");
                txt.Append("<td width='5px' align='center' valign='middle'><img src='../images/png.gif' width='4' height='7' /></td>");
                txt.Append("<td align='left'>&nbsp;&nbsp;<a href='pngDetail.aspx?sType=1096&sClass=1' class='link01'  target='_blank'>时尚经典中英文常用模板</a></td>");
                txt.Append("</tr>");
                txt.Append("</table>");
                txt.Append("</div>");
                txt.Append("</td>");
 
                e.Item.Controls.Add(new System.Web.UI.LiteralControl(txt.ToString()));
            }

        }

用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号 可人软件设计