【问题标题】:display items in repeater unique per rows?在中继器中显示每行唯一的项目?
【发布时间】:2018-03-14 08:48:57
【问题描述】:

我有中继器,我想显示所有项目,如下面的输出,但所有项目都将重复,每行应该是唯一的。 它应该是首先显示两个大列下一行显示四个小 列 这段代码有什么问题我是asp.net网络表单的初学者

 <asp:Repeater ID="rp1" runat="server" DataSourceID="sdsItemsFilter">

             <ItemTemplate>


            <%If counter1 Mod 2 = 1 Then %>

            <div style="margin-top:10px;margin-bottom:10px">


                <%If counter1 Mod 2 = 1 Then %>
                <%for i As Int32 = 1 To 2 %>

            <div style="display:inline-block;width:300px;height:150px;border:1px solid #808080;padding:20px">


             <h1>Item <%#Eval("ItemName")%></h1>



            </div>


            <%Next %>

 <%End If %>
            </div>


            <%End If %>


            <br />  <br />

            <%If counter1 Mod 2 = 0 Then %>
            <div style="margin-top:10px;margin-bottom:10px">



                  <%for i As Int32 = 1 To 4 %>
            <div style="display:inline-block;width:150px;height:150px;border:1px solid #808080;padding:20px">


             <h1>Item <%#Eval("ItemName")%></h1>


            </div>


                <%next %>


            </div>
             <%End If %>


                 <%counter1 += 1 %>

 </ItemTemplate>

         </asp:Repeater>

【问题讨论】:

  • 期望的输出是什么?

标签: asp.net webforms repeater


【解决方案1】:

这里有几个问题。

在下面的代码中,第二个If 块中没有任何意义,因为您只是在检查您在上面检查过的相同条件,因此它始终为真。

    <%If counter1 Mod 2 = 1 Then %>
        <div style="margin-top:10px;margin-bottom:10px">
            <%If counter1 Mod 2 = 1 Then %>

项目不是唯一的,因为您没有在 for 循环中增加 counter1。您的意思是改用循环变量吗?

<h1>Item <%=i%></h1>

【讨论】:

  • 对不起,我需要显示

    Item

    而不是

    Item

【解决方案2】:

可以在repeater中使用ItemDataBound函数,也可以根据条件设置其中的值。

  <asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
<HeaderTemplate>
         <table border="1">
            <tr>
               <td><b>Product</b></td>
               <td><b>Consumer Rating</b></td>
            </tr>
      </HeaderTemplate>

      <ItemTemplate>
         <tr>
            <td> <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' Runat="server"/> </td>
            <td> <asp:Label id="RatingLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Rating") %>' Runat="server"/> </td>
         </tr>
      </ItemTemplate>

      <FooterTemplate>
         </table>
      </FooterTemplate>

     public void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
     {
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == 
  ListItemType.AlternatingItem) {

         if (((Evaluation)e.Item.DataItem).Rating == "Good") {
            ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
         }
      }

}

像这个例子你可以绑定不同的数据。

【讨论】:

  • 对不起 Sumit,但我需要输出像问题中的图像。
猜你喜欢
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
相关资源
最近更新 更多