asp.net每3个结果在转发器上包装一个div

时间:2014-04-10 12:12:18

标签: c# asp.net

我不做asp.net,但是我所在的项目是内置的,如果这是一个非常简单的问题,那就很抱歉:

以下代码将结果拉得很好但我想在可能的情况下每3个结果添加一个包装

<asp:Repeater ID="rptPendingCourses" runat="server" OnItemDataBound="rptPendingCourses_ItemDataBound">
    <ItemTemplate>
        <div class="coursesContainer">
            <div class="coursesContent as">
                <p class="title"><a onclick="linkcourse('<%#DataBinder.Eval(Container.DataItem, "CourseID")%>');return false;" href="#" title='Launch <%# DataBinder.Eval(Container.DataItem, "CourseTitle")%>'><%# System.Web.HttpUtility.HtmlEncode((String)(DataBinder.Eval(Container.DataItem, "CourseTitle").ToString().Length > 25 ? DataBinder.Eval(Container.DataItem, "CourseTitle").ToString().Remove(22) + "..." : DataBinder.Eval(Container.DataItem, "CourseTitle")))%></a></p>
                <ajax:Rating ID="courseRating" runat="server" Visible="false" MaxRating="5" ReadOnly="true"BackColor="Transparent"StarCssClass="ratingStar png" EmptyStarCssClass="emptyRatingStar png" WaitingStarCssClass="waitingRatingStar png" FilledStarCssClass="filledRatingStar png" />
                                <div class="clear"></div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

所以希望它包装每个3个结果,所以就像

<div class="courseWrapper">
  <div class="courseContainer">......</div>
  <div class="courseContainer">......</div>
  <div class="courseContainer">......</div>
</div>

<div class="courseWrapper">
  <div class="courseContainer">......</div>
  <div class="courseContainer">......</div>
  <div class="courseContainer">......</div>
</div>

提前致谢

CS代码:

/* Pending Courses */
rptPendingCourses.DataSource = pendingCourses();
rptPendingCourses.DataBind();

public DataSet pendingCourses()
    {
        DataSet dataSet = new DataSet();
        User user = (User)Context.Items["CurrentUser"];

        SqlConnection selectConnection = new SqlConnection(ConfigurationSettings.AppSettings["DBConnectStr"]);
        SqlDataAdapter adapter = new SqlDataAdapter("dbo.procCataloguesGetAllCoursesByRating", selectConnection);
        adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

        // get results
        adapter.SelectCommand.Parameters.Add("@FilterByDomain", SqlDbType.Bit).Value = 0;
        if (user.Domain.Guid != Guid.Empty) {
            adapter.SelectCommand.Parameters.Add("@DomainID", SqlDbType.UniqueIdentifier).Value = user.Domain.Guid;
        }
        adapter.SelectCommand.Parameters.Add("@Culture", SqlDbType.VarChar, 6).Value = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
        adapter.SelectCommand.Parameters.Add("@IsEnabled", SqlDbType.Bit).Value = 1;
        adapter.SelectCommand.Parameters.Add("@DomainAdminID", SqlDbType.UniqueIdentifier).Value = Guid.Empty;

        try
        {
            dataSet = new DataSet();
            adapter.Fill(dataSet);
        }
        catch (Exception exception)
        {
            dataSet.Dispose();
            dataSet = null;
            LMS_DB.LMS_DB.LogErrorEvent(exception.Message, AuditEntryType.CatalogueCoursesGetCourses);
        }
        finally
        {
            if (selectConnection.State == ConnectionState.Open)
            {
                selectConnection.Close();
            }
        }
        return dataSet;
    }

protected void rptPendingCourses_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView dataItem = (DataRowView)e.Item.DataItem;
        if (Convert.ToBoolean(dataItem.Row["RatingsEnabled"]))
        {
            Rating rating = (Rating)e.Item.FindControl("courseRating");
            rating.Visible = true;
            rating.CurrentRating = Convert.ToInt32(dataItem.Row["AverageRating"]);
        }

    }

1 个答案:

答案 0 :(得分:4)

如果使用ListView控件显示数据,则可以选择指定<GroupTemplate>以及GroupItemCount属性:

<asp:ListView
        GroupItemCount="3" 
        ID="rptPendingCourses"
        runat="server"
        OnItemDataBound="rptPendingCourses_ItemDataBound">
    <LayoutTemplate>
        <div runat="server" ID="groupPlaceholder"></div>
    </LayoutTemplate>
    <GroupTemplate>
        <div class="courseWrapper">
            <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
        </div>
    </GroupTemplate>
    <ItemTemplate>
    <div class="coursesContainer">
        <div class="coursesContent as">
            <p class="title"><a onclick="linkcourse('<%#DataBinder.Eval(Container.DataItem, "CourseID")%>');return false;" href="#" title='Launch <%# DataBinder.Eval(Container.DataItem, "CourseTitle")%>'><%# System.Web.HttpUtility.HtmlEncode((String)(DataBinder.Eval(Container.DataItem, "CourseTitle").ToString().Length > 25 ? DataBinder.Eval(Container.DataItem, "CourseTitle").ToString().Remove(22) + "..." : DataBinder.Eval(Container.DataItem, "CourseTitle")))%></a></p>
            <ajax:Rating ID="courseRating" runat="server" Visible="false" MaxRating="5" ReadOnly="true" BackColor="Transparent" StarCssClass="ratingStar png" EmptyStarCssClass="emptyRatingStar png" WaitingStarCssClass="waitingRatingStar png" FilledStarCssClass="filledRatingStar png" />
                            <div class="clear"></div>
        </div>
    </div>
    </ItemTemplate>
</asp:ListView>

然后,对于OnItemDataBound事件,我认为您需要将其更改为:

protected void rptPendingCourses_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        ListViewDataItem listItem = (ListViewDataItem)e.Item;
        DataRowView dataItem = (DataRowView)listItem.DataItem;
        if (Convert.ToBoolean(dataItem.Row["RatingsEnabled"]))
        {
            Rating rating = (Rating)e.Item.FindControl("courseRating");
            rating.Visible = true;
            rating.CurrentRating = Convert.ToInt32(dataItem.Row["AverageRating"]);
        }
    }
}