使用for循环实现Datalist的分列显示功能

/// <summary>
        /// 工程业绩--用for循环代替了DataList多列显示,得到2行四列的表格,需要内存表的8行数据
        /// </summary>
        private void GcyjShow()
        {
            StringBuilder sb = new StringBuilder();
            ProductBLL pb = new ProductBLL();
            DataTable dt = pb.GetProductByMoreConditions("工程业绩", "工程业绩", "启用");
            int count = dt.Rows.Count;//行数

          sb.Append("<table style='display: inline; vertical-align: middle;' cellpadding='5' align='center'>");//俩行四列的表格
            sb.Append("<tr>");
            for (int i = 0; i < count; i++)
            {
                DataRow dr = dt.Rows[i];
                string strId = dr["ProdId"].ToString();
                string strUrl = dr["Picture"].ToString();
                string strName = dr["ProdName"].ToString();
                if (dr != null)
                {
                    sb.Append("<td>");
                    sb.Append("<table border='0' cellspacing='0' cellpadding='0' width='100%'>");
                    sb.Append("<tr>");
                    sb.Append("<td bgcolor='#ffffff'>");


                    if (!string.IsNullOrEmpty(strId) && !string.IsNullOrEmpty(strName) && !string.IsNullOrEmpty(strUrl))
                    {
                        sb.Append("<a title='/" + strName + "'");
                        sb.Append(" href='GcyjShow.aspx?strId=" + strId + "'>");
                        sb.Append("<img border='0' width='120' height='98' alt='");
                        sb.Append(strName + "'");
                        sb.Append(" src='/Photo/");
                        sb.Append(strUrl + "'>");
                        sb.Append("</a>");
                    }
                    sb.Append("</td>");
                    sb.Append("</tr>");
                    sb.Append("<tr>");
                    sb.Append("<td>");
                    sb.Append("<div style='padding-left: 15px; background: url(Images/index_r1_c9.jpg) no-repeat'>");
                    if (!string.IsNullOrEmpty(strName))
                    {
                        if (strName.Length > 7)
                            sb.Append(strName.Substring(0, 7) + "...");
                        else
                            sb.Append(strName);
                    }
                    sb.Append("</div>");
                    sb.Append("</td>");
                    sb.Append("</tr>");
                    sb.Append("</table>");
                    sb.Append("</td>");
                }
                //我们把内存表的4行数据,当做4列一行来输出
                //如果内存表行数count<=4,即小于我们要显示的一行一列,会自动终止循环,不需要对tr进行处理
                //在等数据等于四列(即一行),且大于一行时    --才进行换行
                if (i == 3 && count > 4)
                {
                    sb.Append("</tr><tr>");
                }
                if (i == 7)//当循环达到8次时,一个俩行四列的表格已经完成,此时终止循环
                {
                    break;
                }
            }

            sb.Append("</tr>");
            sb.Append("</table>");
            lblGcyj.Text = sb.ToString();//用Label标签来输出
        }

转载于:https://www.cnblogs.com/duwolfcn/articles/1930521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值