gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:19:24 24 4
gpt4 key购买 nike

我不做 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"]);
}

}

最佳答案

如果您使用 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"]);
}
}
}

关于c# - asp.net 每 3 个结果在转发器上包装一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22987697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com