[英]How to hide a column in a repeater?
在這里,我發現有一次代碼可以輕松隱藏轉發器列。 這樣效果很好。
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="label1" /></td>
<% if (MustBeVisible) { %>
<td"><asp:Label runat="server" ID="label2" /></td>
<% } %>
</tr>
</ItemTemplate>
但是現在,我需要對TableRow應用CLASS並將其設置為runat =“ server”,以便在ItemDataBound中應用顏色條件,但是當我添加runat =“ server”的屬性時,在運行時會發生沖突,並且警告。
ASP.NET運行時錯誤:在這種情況下不支持代碼塊
這個想法是,例如,在ItemDataBound中評估label1,如果為true,則必須在TR上應用Class使其變為灰色。
關於最佳方法或如何解決此問題的想法?
方法1:
首先,定義一個布爾屬性,在數據項類中(如果可能)說ShouldBeGreyed
。 此屬性應返回該數據項是否將變灰。
然后,在中繼器標記中使用它:
<ItemTemplate>
<tr<%# ((bool)Eval("ShouldBeGreyed"))?"class='grey'":"" %>>
...
</ItemTemplate>
方法二:
首先,在代碼背后定義一個方法,例如ShouldBeGreyed
,如下所示:
protected bool ShouldBeGreyed(object item)
{
// cast to your data-item
var dataItem = (<class-of-your-data-item>)item;
// Determine if it should be greyed out
// bool shouldBeGreyed = ...
...
return shouldBeGreyed;
}
現在,在中繼器標記中使用它:
<ItemTemplate>
<tr<%# ((bool)ShouldBeGreyed(Container.DataItem))?"class='grey'":"" %>>
...
</ItemTemplate>
使用類似這樣的方法。將方法寫在標簽的“可見”部分。
<td><asp:Label runat="server" ID="label2" Visible="<%# MustBeVisible() %>" /></td>
如果您想讓td
不可見/可見,請使用此
<td runat="server visible="<%# MustBeVisible() %>"><asp:Label runat="server" ID="label2" /></td>
如果您制作了<td id="tablerow" runat="server"/>
可以使用以下方法:
tablerow.Attributes.Add("class", className);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.