C# .NET 中继器 HeaderTemplate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1470472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:51:42  来源:igfitidea点击:

.NET Repeater HeaderTemplate

c#.netasp.netvb.net

提问by Antony Delaney

Is there a way to access the field header name of a databound repeater within the header template. So insted of this....

有没有办法在标题模板中访问数据绑定转发器的字段标题名称。太喜欢这个了....

                      <HeaderTemplate>
                          <table >
                              <th ></th>
                              <th >Forename</th>
                              <th >Surname</th>
                              <th >work email</th>
                              <th ></th>
                      </HeaderTemplate>

     We get something like this.                 

                      <HeaderTemplate>
                          <table >
                              <th ></th>
                              <th ><%# Eval("Forename").HeaderName%></th>
                              <th ><%# Eval("SureName").HeaderName%></th>
                              <th ><%# Eval("WorkEmail").HeaderName%></th>
                              <th ></th>
                      </HeaderTemplate>

采纳答案by Per Erik Stendahl

Trying to do Eval("Field").Propertyin the header template would throw a null exception.

尝试在标头模板中执行Eval("Field").Property会抛出空异常。

I would do something like this...

我会做这样的事情......

Code Behind

背后的代码

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<SomeData> data = new List<SomeData>();

        data.Add(new SomeData("Bob"));
        data.Add(new SomeData("Joe"));

        Repeater1.DataSource = data;
        Repeater1.DataBind();
    }

    public String FirstnameColumn {
        get { return "Firstname";  } 
    }
}

public class SomeData
{
    public String Firstname { get; set; }

    public SomeData(String firstname) {
        this.Firstname = firstname;
    }
}

Markup

标记

<table>
    <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <tr><td><%= FirstnameColumn %></td></tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr><td><%# Eval("Firstname") %></td></tr>
        </ItemTemplate>
    </asp:Repeater>        
</table>

回答by veggerby

You cannot use the <%# %>syntax in the HeaderTemplate because you it is not in a databinding scenario. You should however be able to use <%= %>and then put some method on your page/usercontrol that returns the Header.

您不能使用<%# %>HeaderTemplate 中的语法,因为它不在数据绑定方案中。但是,您应该能够使用<%= %>然后在返回页眉的页面/用户控件上放置一些方法。

回答by Per Erik Stendahl

You could move the table header into your ItemTemplate like this:

您可以像这样将表格标题移动到您的 ItemTemplate 中:

<ItemTemplate>
    <asp:Panel runat="server" Visible='<%# Container.DisplayIndex == 0 %>'>
        <tr>
            <th><%# Eval("Forename").HeaderName %></th>
        </tr>
    </asp:Panel>
    <tr>
        <td><%# Eval("Forename") %></td>
    </tr>
</ItemTemplate>

although this is slightly wasteful since the header would be bound for each row (only the first shown though). Perhaps it would be better to use <% if (...) %> instead of a Panel but I don't know how to access the Container.DisplayIndex in that context.

尽管这有点浪费,因为标题将绑定到每一行(尽管只显示了第一行)。也许使用 <% if (...) %> 而不是 Panel 会更好,但我不知道如何在该上下文中访问 Container.DisplayIndex 。

Edit:

编辑:

In .net 4.5 Container.DisplayIndexdoes not work; replace with Container.ItemIndex.

在 .net 4.5Container.DisplayIndex中不起作用;替换为Container.ItemIndex.

Full example:

完整示例:

<ItemTemplate>
    <asp:Panel runat="server" Visible='<%# Container.ItemIndex == 0 %>'>
        <tr>
            <th><%# Eval("Forename").HeaderName %></th>
        </tr>
    </asp:Panel>
    <tr>
        <td><%# Eval("Forename") %></td>
    </tr>
</ItemTemplate>

回答by Laki Politis

Why don't you use the Item Type already found in the RepeaterEvents?

为什么不使用已在 RepeaterEvents 中找到的项目类型?

    protected void rptContent_DataBound(object Sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            var x= e.Item.FindControl("xxx") as Label;
             ...
        }
    }