1

我想让我的转发器中的项目在一个 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>

我怎样才能做到这一点?提前致谢!

4

2 回答 2

3

使用LiteralRepeater 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>

预期结果如下:

  1. 的文本ltDivPrefix<div class="column">用于第 n 个项目,其中 n % 5 等于 0,否则将为空。
  2. 的文本ltDivSuffix</div>用于第 n 个项目,其中 n % 5 等于 4,或者 n 是最后一个索引,否则将为空。


在 event 中可以获取 item 的总数PreRender,所以对 Repeater 的 items 进行循环,ltDivPrefixltDivSuffix根据 item 索引设置 and 的文本:

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>";
        }
    }
}
于 2014-01-23T15:27:21.007 回答
0

有什么理由不能将 引入div模板?

  <ItemTemplate>
     <div class="column">
       <asp:HiddenField runat="server" ID="eventID" />
       <p><asp:CheckBox runat="server" ID="eventDateCheckbox" /></p>
     </div>
  </ItemTemplate>
于 2014-01-23T15:27:34.317 回答