关闭

数据绑定控件显示数据换行的几个实例

标签: datagridjavascriptclasshtmlserverborder
592人阅读 评论(0) 收藏 举报

不知不觉做web开发已经2年了,在这两年里我干的最多的活就是数据绑定。我个人比较喜欢用datagrid,我最开始接触的就是datagrid,然后是repeater,然后是datalist、gridview等。有些时候客户要求特定的格式显示数据,比如数据横向排开,一行显示几列等。这就是典型的控件应用变通,有些不让用任何控件的我就用代码在后台写,然后将形成的html发送给前台的html控件。

废话不多说,拿数据横向排开,一行显示2列来做个例子。

repeater:

<table cellpadding="0" border="0" cellspacing="0" width="100%" id="tblData" class="tblJhwc">
          <tr class="trHead">
              <td>单位</td>
              <td>计划</td>
              <td>完成</td>
              <td width="66">百分比</td>
              <td>单位</td>
              <td>计划</td>
              <td>完成</td>
              <td width="66">百分比</td>
            </tr>
            <tr>
      <asp:Repeater runat="server" ID="rptData" OnItemDataBound="rptData_ItemDataBound">
     <ItemTemplate>
         <td><%# Eval("dwmc") %></td>
         <td sm='计划|<%# Eval("jhqk").ToString().Trim()%>' class="tipTd"><%# Eval("jh") %></td>
         <td sm='已完成|<%# Eval("wcqk").ToString().Trim()%>' class="tipTd"><%# Eval("wc")%></td>
         <td><%# Eval("wcbl")%></td>
     </ItemTemplate>
     </asp:Repeater>
     </tr>
        </table>

 

后台:

 //分两列显示
        protected void rptData_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemIndex+1) % 2 == 0)
            {
                e.Item.Controls.Add(new LiteralControl("</tr><tr>"));
            }
        }

 

datalist就比较简单点了

<asp:DataList ID="DList_Nbrj" RepeatDirection="Horizontal" RepeatColumns="2" ShowFooter="false" ShowHeader="false" BorderWidth="0" runat="server">
       <ItemTemplate>
           <a title='<%#DataBinder.Eval(Container.DataItem, "SoftName")%>' href="javascript:void(0)" onclick="openNbrjLink('<%# DataBinder.Eval(Container.DataItem, "SoftLink") %>',this)" >
       <%# DataBinder.Eval(Container.DataItem, "SoftImgPath").ToString() == "" ? DataBinder.Eval(Container.DataItem, "SoftName") : DataBinder.Eval(Container.DataItem, "SoftImg")%></a>
          </ItemTemplate>
     </asp:DataList>

 

属性RepeatDirection是重复方向,RepeatColumns是显示的列数。

 

手写的html代码

前台我用 <asp:Literal runat="server" ID="litTable"></asp:Literal>显示html内容

后台:

int cols = 2;//一行显示的单位个数
                string tableHtml = "<table cellspacing='0' cellpadding='2' border='0' ><tr>";
                string imgHtml = "";
                DataRow[] drData = dtData.Select("sjdwdm= '" + sjdwdm + "'");
                //Response.Write("bbbbb");
                for (int i = 0; i < drData.Length; i++)
                {
                    if (drData[i]["sl"].ToString() == "0")//未上报
                    {
                        //tips = "title='未上报'";
                        imgHtml = "<img src='../images/redCross.gif'>";//红X
                    }
                    else//上报了(dt.Rows[i]["sl"].ToString()不会是空)
                    {
                        if (drData[i]["cb"].ToString() == "1")//迟报
                        {
                            if (drData[i]["cfsb"].ToString() == "1")//重复上报
                            {
                                //tips = "title='迟报且重复上报'";
                                imgHtml = "<img src='../images/dblgreenFlag.gif'>";//绿绿旗
                            }
                            else//没有重复上报
                            {
                                //tips = "title='迟报'";
                                imgHtml = "<img src='../images/greenFlag.gif'>";//绿旗
                            }
                        }
                        else//正常上报
                        {
                            if (drData[i]["cfsb"].ToString() == "1")//重复上报
                            {
                                //tips = "title='重复上报'";
                                imgHtml = "<img src='../images/dblredFlag.gif'>";//红红旗
                            }
                            else
                            {
                                //tips = "title='正常上报'";
                                imgHtml = "<img src='../images/redFlag.gif'>";//红旗
                            }

                        }
                    }
                    tableHtml += "<td width='60' align='left' onmouseover='javascript:this.className=/"tdOver/"' onmouseout='javascript:this.className=/"tdOut/"' class='tdOut' onclick='setLink(/"" + drData[i]["dwdm"] + "/",/"" + drData[i]["dwscgs"] + "/",/"" + tablename + "/",/"" + rq.Replace("-", "") + "/")' title='每天上报时间:" + drData[i]["mtsbsj"] + "'>" + drData[i]["dwscgs"] + "</td><td align='left' width='45'>" + imgHtml + "</td>";
                    if ((i + 1) % cols == 0)
                        tableHtml += "</tr><tr>";
                }
                if (drData.Length % cols > 0)//补齐最后一行的单元格
                {
                    int len = cols - (drData.Length % cols);
                    for (int j = 0; j < len; j++)
                    {
                        tableHtml += "<td width='60'>&nbsp;</td><td width='45'>&nbsp;</td>";
                    }
                }
                tableHtml += "</tr></table>";

 

手写的代码从执行效率上说应该比控件快点,毕竟不用控件先解析出html代码。先总结这些,以后再修改。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9883次
    • 积分:185
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档