C# 隐藏(使不可见)转发器页脚和/或页眉
我是否可以在中继器中隐藏页脚和/或页眉 基本上,我有两个中继器绑定到不同的数据源,一个在另一个之上:C# 隐藏(使不可见)转发器页脚和/或页眉,c#,asp.net,webforms,repeater,C#,Asp.net,Webforms,Repeater,我是否可以在中继器中隐藏页脚和/或页眉 基本上,我有两个中继器绑定到不同的数据源,一个在另一个之上: <asp:Repeater runat="server" ID="rptAdditionalCosts"> <HeaderTemplate> <table id="optionalExtrasPriceBreakdown" cellpadding="0" cellspacing="0" border="0">
<asp:Repeater runat="server" ID="rptAdditionalCosts">
<HeaderTemplate>
<table id="optionalExtrasPriceBreakdown" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th colspan="4"> Additional Costs </th>
</tr>
<tr>
<th> Description </th>
<th> Quantity </th>
<th> Price </th>
<th class="totalPrice"> Total Price </th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Count")%></td>
<td>£<%# DataBinder.Eval(Container.DataItem, "Gross", "{0:n2}")%></td>
<td class="totalPrice">£<%#DataBinder.Eval(Container.DataItem, "Total", "{0:n2}")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
<hr class="spacer" />
</FooterTemplate>
</asp:Repeater>
<asp:Repeater runat="server" ID="rptOptionalExtras">
<HeaderTemplate>
<table id="optionalExtrasPriceBreakdown" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th colspan="4"> Additional Costs </th>
</tr>
<tr>
<th> Description </th>
<th> Quantity </th>
<th> Price </th>
<th class="totalPrice"> Total Price </th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Number")%></td>
<td>£<%# DataBinder.Eval(Container.DataItem, "UnitCost", "{0:n2}")%></td>
<td class="totalPrice">£<%#DataBinder.Eval(Container.DataItem, "TotalCost", "{0:n2}")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
<hr class="spacer" />
</FooterTemplate>
</asp:Repeater>
问题是中继器似乎不包含任何隐藏或显示页脚或页眉模板的选项?我是否遗漏了一些明显的内容?尝试将页眉和页脚模板分别设置为null,如下所示:
if (optionalVisible && additionalVisible)
{
//hide rptAdditionalCosts footer
//and
//hide rptPerBookingOptionalExtras header
rptAdditionalCosts.FooterTemplate = null;
rptPerBookingOptionalExtras.HeaderTemplate = null;
}
最后,我使用中继器的ItemDataBound事件成功地实现了一些功能:
void rptAdditionalCosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (optionalVisible && additionalVisible)
e.Item.Visible = false;
}
}
void rptOptionalExtras_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
if (optionalVisible && additionalVisible)
e.Item.Visible = false;
}
}
多亏了@KarlAnderson的回答,我从来没有真正检查过他的解决方案,因为我刚刚运行了它。我在将模板设置为null时遇到了问题,会干扰转发器命令。我建议改为设置一个新的BindableTemplateBuilder
if (optionalVisible && additionalVisible)
{
//hide rptAdditionalCosts footer
//and
//hide rptPerBookingOptionalExtras header
rptAdditionalCosts.FooterTemplate = new BindableTemplateBuilder();
rptPerBookingOptionalExtras.HeaderTemplate = new BindableTemplateBuilder();
}
您可以使用JQuery执行if语句,请参见:我不想使用JQuery解决方案,谢谢。这不起作用。但下面的ItemDataBound答案确实如此。
if (optionalVisible && additionalVisible)
{
//hide rptAdditionalCosts footer
//and
//hide rptPerBookingOptionalExtras header
rptAdditionalCosts.FooterTemplate = new BindableTemplateBuilder();
rptPerBookingOptionalExtras.HeaderTemplate = new BindableTemplateBuilder();
}