Repeat a Repeater Control?

Last post 08-08-2008 5:11 PM by barryman9000. 7 replies.

Sort Posts:

  • Repeat a Repeater Control?

    08-06-2008, 5:57 PM
    • Participant
      1,172 point Participant
    • barryman9000
    • Member since 01-24-2006, 1:58 PM
    • Santa Barbara CA
    • Posts 482

    I have a stored procedure that returns one big table with Employee names, phone numbers, and their state.  I need to generate different tables (or GridViews, or Repeaters... not sure) for each state.  Here's an example:

    My table has
    ID     Employee_Name    Phone_Number     State

    I need my web form to look like this:

    California
    ID    Employee_Name   Phone_Number
    ID    Employee_Name   Phone_Number
    ID    Employee_Name   Phone_Number
    ID    Employee_Name   Phone_Number

    Arizona
    ID    Employee_Name   Phone_Number
    ID    Employee_Name   Phone_Number
    ID    Employee_Name   Phone_Number
    ID    Employee_Name   Phone_Number

    I've seen some posts regarding repeating a GridView (actually DataGrid, but I'm in 3.5) but I'm not sure I understand how to group that data by State.  Any suggestions?  Thanks in advance

  • Re: Repeat a Repeater Control?

    08-06-2008, 6:28 PM

    What you need to use is the list view control which is new to asp.net 3.5, this tutorial is almost exactly what you are after http://aspnet.4guysfromrolla.com/articles/122607-1.aspx. Although I would save the data from your database into a list and then query it with LINQ. Let me know if you have any trouble getting it working.

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: Repeat a Repeater Control?

    08-06-2008, 7:12 PM
    • Participant
      1,172 point Participant
    • barryman9000
    • Member since 01-24-2006, 1:58 PM
    • Santa Barbara CA
    • Posts 482

    Thanks for the response.  I might need some more help with this.  I'm still not sure how to group my data into individual repeater type controls on the page.  The DataTable that I have has records for employees from all states.  I need to take that data, and generate a repeater-looking control that lists employees for each State.  Can I do that with the ListView?  It looks like the header is static in this example, and my code is just returning a list, much like the Repeater.

    Let me know if you need more info.  Thanks again.

     

  • Re: Repeat a Repeater Control?

    08-06-2008, 11:49 PM
    • Participant
      1,172 point Participant
    • barryman9000
    • Member since 01-24-2006, 1:58 PM
    • Santa Barbara CA
    • Posts 482
  • Re: Repeat a Repeater Control?

    08-07-2008, 8:37 AM
    If you can get your recordset into a list you can use LINQ like so:

    HTML
    <asp:ListView ID="ListView1" runat="server">
            
    <LayoutTemplate>
                   
    <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
            
    </LayoutTemplate>
            
    <ItemTemplate>
                   
    <table>
                        
    <tr>
                            
    <td><%#Eval("Key")%></td>
                        
    </tr> 
                         
    <tr>
                            
    <td>
                                
    <ol>
                                   
    <asp:ListView ID="ListView2" runat="server" DataSource='<%# Eval("emps") %>' >
                                         
    <LayoutTemplate>
                                               
    <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
                                         
    </LayoutTemplate>
                                         
    <ItemTemplate>
                                                
    <li><%#Eval("Name")%></li>
                                         
    </ItemTemplate>
                                   
    </asp:ListView>
                               
    </ol>
                           
    </td>
                       
    </tr>
                  
    </table>
                  
    <hr />
             
    </ItemTemplate>
    </asp:ListView>

    Code Behind
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

             
    Dim empsByLocation = _
                     
    From em In Employee.getEmployees() _
                     
    Group em By Key = em.Location Into Group _
                     
    Select New With {.Key = Key, .emps = Group}

              ListView1.DataSource = empsByLocation
              ListView1.DataBind()
    End Sub

    My Employee Class
    Public Class Employee

         
    Public _Location As String
          
    Public _Name As String
         
    Public _Phone As String
         
    Public _ID As Long

     

          Public Sub New(ByVal ID As Long, ByVal Name As String, ByVal Phone As String, ByVal Location As String)
              
    Me._Location = Location
              
    Me._Name = Name
              
    Me._Phone = Phone
               
    Me._ID = ID
         
    End Sub

          Public ReadOnly Property Name() As String
             
    Get
                  
    Return _Name
             
    End Get
         
    End Property

          Public ReadOnly Property Location() As String
            
    Get
                 
    Return _Location
             
    End Get
         
    End Property

          Public Shared Function getEmployees() As List(Of Employee)
     
               
    Dim employees As New List(Of Employee)
     
                employees.Add(
    New Employee(1, "Bob", "09809809", "California"))
                employees.Add(
    New Employee(2, "Jack", "3453465", "California"))
                employees.Add(
    New Employee(3, "Arnold", "7899878978", "California"))
                employees.Add(
    New Employee(4, "Barry", "2134678", "Arizona"))
                employees.Add(
    New Employee(5, "Titch", "454458", "Arizona"))
                employees.Add(
    New Employee(6, "Mick", "23432424", "Arizona"))

               
    Return employees
         
    End Function

    End Class

    Will produce the output:

    California
    1. Bob
    2. Jack
    3. Arnold

    Arizona
    1. Barry
    2. Titch
    3. Mick



    You can tidy it up but I hope this was what you were after.
    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: Repeat a Repeater Control?

    08-07-2008, 12:25 PM
    Answer
    • Participant
      1,172 point Participant
    • barryman9000
    • Member since 01-24-2006, 1:58 PM
    • Santa Barbara CA
    • Posts 482

    Wow thanks.  I'm going to give it a try. 

    Any change you can give me that Linq code in C#?  I can't find any code converters online that understand the Linq portion, and my version isn't working

     

    var results = (from RE in DbDocument.GetSearchResults(type, _subType)
                                  group RE by SUBTYPE into g
                                  select new {.Key = Key, .emps = group});
     It doesn't like the last line.  Is the issue with my DbDocument.GetSearchResults method?  It returns a DataTable.
  • Re: Repeat a Repeater Control?

    08-07-2008, 2:49 PM
    Answer

    You won't be able to use Linq with a datatable, you need a collection that implements IEnumerable. Here is an article to get around it - http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/01/15/284.aspx, and its in C# Smile

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: Repeat a Repeater Control?

    08-08-2008, 5:11 PM
    • Participant
      1,172 point Participant
    • barryman9000
    • Member since 01-24-2006, 1:58 PM
    • Santa Barbara CA
    • Posts 482

    I ended up using that old school method (which works really well, considering it's an article from 2003) but I'll definitely try your method next time around.

    Thanks again for all your help!

Page 1 of 1 (8 items)