Skip to main content
 首页 » 编程设计

c#之隐藏asp Repeater的HeaderTemplate

2024年02月13日15JeffreyZhao

我想在我的 asp.net 页面的 headerTemplate block 中隐藏一些行 这是我的 apsx 页面代码:

<HeaderTemplate> 
  <table class="uk-table uk-table-hover uk-table-striped"> 
     <thead> 
        <tr> 
            <th class="uk-width-1-10">numbr 1</th> 
            <th class="uk-width-1-10">numbr 2</th> 
            <th class="uk-width-2-10">numbr 3</th>                                             
        </tr> 
    </thead> 
    <tbody>                                                                                        
</HeaderTemplate>                                             
<ItemTemplate> 
...  

然后我改成隐藏数字 3

<HeaderTemplate> 
  <table class="uk-table uk-table-hover uk-table-striped"> 
     <thead> 
        <tr> 
            <th class="uk-width-1-10">numbr 1</th> 
            <th class="uk-width-1-10">numbr 2</th> 
            <th class="uk-width-2-10"><asp:Label runat="server" ID="tdInfoHeader" Visible="true">numbr 3</asp:Label></th>                                             
        </tr> 
    </thead> 
    <tbody>                                                                                        
</HeaderTemplate>                                             
<ItemTemplate> 
...      

但是在我的 C# 代码部分,我无法更改 tdInfoHeader.visible =false,我怎么能在我的 C# 部分隐藏数字 3?

请您参考如下方法:

您可以使用 Repeater.ItemDataBound实现这一目标的事件。

首先,确保将 OnItemDataBound 属性设置为您的 aspx 代码中事件处理程序方法的名称,假设名称为 rpt1_ItemDataBound:

<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound"> 

然后在rpt1_ItemDataBound方法中隐藏tdInfoHeader:

protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Header) 
    { 
        Label tdInfoHeader = (Label)e.Item.FindControl("tdInfoHeader"); 
        tdInfoHeader.Visible = false; 
    } 
}