repeater多列显示问题

qinglong332 2012-05-03 09:53:58
想问下repeater多列显示问题
外面加个
int tdIndex = 1;
绑定事件里用
if (tdIndex % 3 == 0)
{
Literal ltrlBr = new Literal();
ltrlBr.Text = "</tr><tr>";
e.Item.Controls.Add(ltrlBr);
}
tdIndex++;
确实可以多列显示,但页面回发后就全部单行显示了,能解决吗?
DataList可以,但感觉只用来显示数据太过浪费,耗时长
...全文
231 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinglong332 2012-05-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

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

一楼的回答怎么一直到结贴的时候才看到的?未结贴之前都没显示过。。。
qinglong332 2012-05-08
  • 打赏
  • 举报
回复
哈哈哈,自己完美解决此问题了:
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++;
}
qinglong332 2012-05-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

用DIV很好处理的,循环DIV。
[/Quote]
div在垂直方向上排序不好排(Repeater嵌套效果不好),下面是用table排列的效果图:


如果改DIV,基本就是顶端对齐了
qinglong332 2012-05-05
  • 打赏
  • 举报
回复
有。。有米有人能告诉我??
tkss 2012-05-05
  • 打赏
  • 举报
回复
用DIV很好处理的,循环DIV。
qinglong332 2012-05-03
  • 打赏
  • 举报
回复
我是2层嵌套的,
那样的话,要重新获取数据源,重新绑定外层Repeater数据
是不是比起DataList就麻烦了?

我就想优化一下,不知在显示时哪个更节约时间(多列)
乘风破浪dgg 2012-05-03
  • 打赏
  • 举报
回复
回发后重新绑定数据啊,要么加上viewstate试试行不。
ice_baili 2012-05-03
  • 打赏
  • 举报
回复
别用变量,页面回发相当于重新打开了页面,变量又被归零了,最好用label,把值保存在label中数据就不容易丢失了~
qinglong332 2012-05-03
  • 打赏
  • 举报
回复
前台代码如下:

<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吗?
会不会太浪费啊?
qinglong332 2012-05-03
  • 打赏
  • 举报
回复
没人解答吗???

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧