想问下repeater多列显示问题
外面加个
int tdIndex = 1;
绑定事件里用
        if (tdIndex % 3 == 0)
        {
            Literal ltrlBr = new Literal();
            ltrlBr.Text = "</tr><tr>";
            e.Item.Controls.Add(ltrlBr);
        }
        tdIndex++;
确实可以多列显示,但页面回发后就全部单行显示了,能解决吗?
DataList可以,但感觉只用来显示数据太过浪费,耗时长

解决方案 »

  1.   

    前台代码如下:<table cellspacing="0" border="0" style="width:106px;border-collapse:collapse;">
     <tr><asp:Repeater ID="rptSections" runat="server" onitemdatabound="rptSections_ItemDataBound">
       <ItemTemplate>
        <td><div style="width:60px; margin-left:2px;">
           <a href="#" style="text-decoration: none; color: #676767;" 
              runat="server" id="sectionLnk"></a>
            </div></td>
       </ItemTemplate>                                           
     </asp:Repeater></tr>
    </table>后台代码:    protected int tdIndex = 1;
        protected void rptSections_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            // 动态获取相栏目信息
            DataRow dtRow = (e.Item.DataItem as DataRowView).Row;
            int id = dtRow.Field<int>("ID");
            string sectionName = dtRow.Field<string>("Name");        // 动态设置相关栏目超链接
            HtmlAnchor sectionLnk = e.Item.FindControl("sectionLnk") as HtmlAnchor;
            sectionLnk.Title = sectionName;
            sectionLnk.HRef = "SectionPage.aspx?id=" + id + "&cityid=" + GetCityId();
            sectionLnk.InnerText = sectionName;        // repeater嵌套使用,内层repeater每次初始置1后操作
            if (e.Item.ItemIndex == 0)
            {
                tdIndex = 1;
            }
            if (tdIndex % 3 == 0)
            {
                Literal ltrlBr = new Literal();
                ltrlBr.Text = "</tr><tr>";
                e.Item.Controls.Add(ltrlBr);
            }
            tdIndex++;
        }
    初始加载确实可行,回发后变成单行显示了,咋整?
    有人说用AlternatingItemTemplate显示2列,确实可以,我就想知道要是超过2列呢?只能用DataList吗?
    会不会太浪费啊?
      

  2.   

    别用变量,页面回发相当于重新打开了页面,变量又被归零了,最好用label,把值保存在label中数据就不容易丢失了~
      

  3.   

    回发后重新绑定数据啊,要么加上viewstate试试行不。
      

  4.   

    我是2层嵌套的,
    那样的话,要重新获取数据源,重新绑定外层Repeater数据
    是不是比起DataList就麻烦了?我就想优化一下,不知在显示时哪个更节约时间(多列)
      

  5.   

    div在垂直方向上排序不好排(Repeater嵌套效果不好),下面是用table排列的效果图:
    如果改DIV,基本就是顶端对齐了
      

  6.   

    哈哈哈,自己完美解决此问题了:
    1.直接把Literal加到了前台,不设置文本值。
    2.后台动态获取Literal控件,当被操作项该换行时,设置其文本值为"</tr><tr>"。
    3.打完收工。嘘。前台:<table cellspacing="0" border="0" style="width:106px;border-collapse:collapse;">
     <tr><asp:Repeater ID="rptSections" runat="server" onitemdatabound="rptSections_ItemDataBound">
       <ItemTemplate>
        <td><div style="width:60px; margin-left:2px;">
           <a href="#" style="text-decoration: none; color: #676767;" 
              runat="server" id="sectionLnk"></a>
        </div></td>
        <asp:Literal ID="ltrlBr" runat="server"></asp:Literal> 
       </ItemTemplate>                                           
     </asp:Repeater></tr>
    </table>
    后台:    protected int tdIndex = 1;
        protected void rptSections_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            // 动态获取相栏目信息
            RepeaterItem rptItem = e.Item;
            DataRow dtRow = (rptItem.DataItem as DataRowView).Row;
            int id = dtRow.Field<int>("ID");
            string sectionName = dtRow.Field<string>("Name");        // 动态设置相关栏目超链接
            HtmlAnchor sectionLnk = rptItem.FindControl("sectionLnk") as HtmlAnchor;
            sectionLnk.Title = sectionName;
            sectionLnk.HRef = "SectionPage.aspx?id=" + id + "&cityid=" + GetCityId();
            sectionLnk.InnerText = sectionName;        // repeater嵌套使用,内层repeater每次初始置1后操作
            if (rptItem.ItemIndex == 0)
            {
                tdIndex = 1;
            }
            if (tdIndex % 3 == 0)
            {
                Literal ltrlBr = rptItem.FindControl("ltrlBr") as Literal;
                ltrlBr.Text = "</tr><tr>";
            }
            tdIndex++;
        }