隐藏asp Repeater的HeaderTemplate

时间:2014-01-20 13:16:13

标签: c# asp.net repeater

我想在asp.net页面的headerTemplate块中隐藏一些行 这是我的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号?

2 个答案:

答案 0 :(得分:1)

试试这个

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

找到您的headerTemplate行并设置visible=false

答案 1 :(得分:1)

您可以使用Repeater.ItemDataBound事件来实现此目标。

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

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

然后在tdInfoHeader方法中隐藏rpt1_ItemDataBound

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