我想把我的转发器中的物品放在div之间 在新div中的5个元素之后。
所以我现在得到的是:
查看:
<ItemTemplate>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
</div>
</ItemTemplate>
代码背后:
protected void DatesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
var data = (Components.Models.GetDatesModel)e.Item.DataItem;
var chk = (CheckBox)e.Item.FindControl("eventDateCheckbox");
chk.Text = data.EventTimeBegin.ToString();
var hfd = (HiddenField)e.Item.FindControl("eventID");
hfd.Value = data.eventID.ToString();
}
}
我希望在视图中显示如下:
<div class="column">
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
</div>
<div class="column">
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
</div>
<div class="column">
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
</div>
我怎样才能做到这一点? 提前谢谢!
答案 0 :(得分:3)
使用Literal,Repeater PreRender event和% Operator的组合。
以下是aspx代码的外观:
<asp:Repeater ID="DatesRepeater" runat="server" OnItemDataBound="DatesRepeater_ItemDataBound"
OnPreRender="DatesRepeater_PreRender">
<ItemTemplate>
<asp:Literal ID="ltDivPrefix" runat="server"></asp:Literal>
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
<asp:Literal ID="ltDivSuffix" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
预期结果如下:
ltDivPrefix
的文字为<div class="column">
,其中n%5等于0,否则为空。ltDivSuffix
的文本将为</div>
,其中n%5等于4,或者n是最后一个索引,否则它将为空。
可以在PreRender
事件中获取项目总数,因此循环执行Repeater项目并根据项目索引设置ltDivPrefix
和ltDivSuffix
的文本:
protected void DatesRepeater_PreRender(object sender, EventArgs e)
{
for (int i = 0; i < DatesRepeater.Items.Count; i++)
{
Literal ltDivPrefix = (Literal)DatesRepeater.Items[i].FindControl("ltDivPrefix");
Literal ltDivSuffix = (Literal)DatesRepeater.Items[i].FindControl("ltDivSuffix");
if ((i) % 5 == 0)
{
ltDivPrefix.Text = @"<div class=""column"">";
}
if ((i % 5 == 4) || (i == DatesRepeater.Items.Count - 1))
{
ltDivSuffix.Text = "</div>";
}
}
}
答案 1 :(得分:0)
有什么理由不能将div
介绍给模板吗?
<ItemTemplate>
<div class="column">
<asp:HiddenField runat="server" ID="eventID" />
<p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
</div>
</ItemTemplate>