【问题标题】:asp:repeater - headers at section changeasp:repeater - 部分更改的标题
【发布时间】:2009-02-03 14:13:17
【问题描述】:

有没有办法在数据绑定 asp:repeater 控件中的字段更改时显示子标题行,例如

而不是

国家 |颜色 |数字 英国 |红色 | 3 英国 |绿色 | 3 法国| 家企业红3

这样做:

==英国== 颜色 |数字 红色 | 3 绿色 3 ==法国== 颜色 |数字 红色 | 3

非常感谢您的帮助。

【问题讨论】:

    标签: asp.net data-binding formatting repeater


    【解决方案1】:

    没有内置支持,但这并不意味着不可能。

    您需要覆盖 OnItemDataBound 事件,并在标记中包含以下内容:

    <asp:Repeater OnItemDataBound="NextItem" ... >
        <ItemTemplate><asp:Literal Id="Header" Visible="False" Text="{0}<strong>{1}</strong><br/><table>" ... />
             <tr>
                 <td><asp:Label id="Color" Text="<%# Eval("Color")" ... /></td>
                 <td><asp:Label id="Number" Text="<%# Eval("Number")" ... /></td>
             </tr>
        </ItemTemplate>
    </asp:Repeater></table>
    

    然后在代码隐藏中:

    private string CurCountry = string.Empty;
    
    private void NextItem(object sender, RepeaterItemEventARgs e)
    {
        if ( e.Item.ItemType != ListItemType.Item 
          && e.Item.ItemType != ListItemType.AlternatingItem) return;
    
        DbDataRecord row = (DbDataRecord)e.Item.DataItem;
    
        if (CurCountry != row["country"].ToString() )
        {
            string prev = (CurCounter == string.Empty)?"":"</table>";
            CurCountry = row["country"].ToString();
    
            Literal header = (Literal)e.Item.FindControl("Header");
            Literal footer = (Literal)e.Item.FindControl("Footer");
    
            header.Text = string.Format(header.Text, prev, CurCountry);
            header.Visible = true;
        }
    }
    

    【讨论】:

      【解决方案2】:

      另一种解决方案是简单地使用两个中继器,一个嵌套在另一个中。您可以将带有子记录的组传递给第一个转发器,然后在组转发器的 ItemDataBound 上,将子记录传递给子转发器并在那里调用 DataBind()。

      这是更多代码,但实际上可以让您更好地控制布局,而无需在代码隐藏中使用 HTML 创建代码。

      正如您在此处看到的,我们有一个父中继器,并且在项目模板中,我们可以根据需要自定义每个组。在 ChildRepeater 中,我们有我们的项目模板,我们可以在其中自定义分组内的每个项目。非常干净,并且全部带有声明性 UI。

      <asp:Repeater runat="server" id="GroupRepeater">
       <ItemTemplate>
         <asp:Literal runat="server" id="HeaderText" />
         <asp:Repeater runat="server id="ChildRepeater">
          <ItemTemplate>
            <asp:Literal runat="server" id="InfoGoesHere" />
          </ItemTemplate>
         </asp:Repeater>
       </ItemTemplate>
      </asp:Repeater>
      

      在后面的代码中我们可以这样写:

      private void GroupRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
        //Get the child records, this can be any data structure you want
        SomeChildCollection children = ((SomeGroupCollection)e.Item.DataItem).Children;
        //Find the child repeater
        Repeater childRepeater = e.Item.FindControl("ChildRepeater") as Repeater;
        childRepeater.ItemDataBound += SomeMethod;
        childRepeater.DataSource = children;
        childRepeater.DataBind();
      }
      

      绑定每个子项后,您可以订阅 ItemDataBound 事件并根据需要将子项绑定到控件。

      【讨论】:

      【解决方案3】:

      对于 joel 解决方案,我可以建议以下 - 更改此行:

      header.Text = string.Format("<strong> {0} </strong><br/><table>", CurCountry);
      

      header.Text = string.Format(header.Text, CurCountry);
      

      您可以从 .as?x 文件自定义外观。

      【讨论】:

        【解决方案4】:

        啊 - 你正在寻找一个分组中继器。我过去改编的solution 似乎消失了。也许你能找到它。但无论如何,搜索分组中继器应该会有所帮助。

        【讨论】:

        • 干杯,这就是我要找的。​​span>
        【解决方案5】:

        dotnetjunkies 代码中似乎有一些错误,但我在这里找到了一个更简单的解决方案 http://www.west-wind.com/weblog/posts/140753.aspx

        现在中继器不支持分组,所以你必须自己做这件事,但这很容易做到。在上面的转发器中,以下数据表达式负责分组:

        <%# this.RenderGroup(Eval("Group") as
        string) %> 
        

        表达式调用表单上的一个方法来呈现一个额外的 div 标签。这个简单方法的代码如下所示:

        string LastGroup = "@#@~";
        protected string RenderGroup(string Group) {   
            if (Group == this.LastGroup)       
            return "";     // *** Group has changed    
            this.LastGroup = Group;    
            return "<div class='groupheader'>" +Group + "</div>";
        }
        

        每个项目都会调用此代码,它只是检查组是否已更改。如果没有返回任何内容,否则返回一个简单的 div 标签作为字符串嵌入到标记中。我选择发回 HTML,但我想你也可以返回 true 或 false 并有条件地在 Repeater 中渲染一个控件,这将使 CSS 纳粹更快乐。

        【讨论】:

          猜你喜欢
          • 2011-03-18
          • 1970-01-01
          • 2011-04-12
          • 2010-10-23
          • 1970-01-01
          • 2015-07-25
          • 2012-11-23
          • 2023-03-31
          • 1970-01-01
          相关资源
          最近更新 更多