1 Replies - 2499 Views - Last Post: 23 July 2010 - 02:14 PM Rate Topic: -----

#1 atognarelli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-July 10

Using a DataControlFieldCollection in a Custom Control

Posted 22 July 2010 - 11:48 AM

I am trying to create a custom data listing that will allow for filtering, paging, sorting and be fully accessible and compliant with standards I need to work within - (for a history of why I'm am going this way as opposed to a GridView or something already integrated within the framework see this post: http://www.dreaminco...1&#entry1070909 - but no javascript is the main issue).

I am not an expert in .Net by any means and I am lost here.

Scenario: I have created a Custom Control using a Repeater control, a DataControlFieldCollection collection and a DataTable.

Issue: DataControlFields are not binding to any data. Essentially, when the page is rendered I have the table showing the appropriate number of cells and any static text that may be there, but no dynamically generated data. Let me layout the code here and maybe it will be apparent to someone what I am doing wrong.

The Repeater Item -- notice the call to DisplayRepeater in the ItemTemplate, this may or may not be the root of the problem but I will leave that up to someone more knowlegeable than I to decide

<asp:Repeater runat="server" ID="CustomDataRepeater">
    <HeaderTemplate>
        <table border="1" width="<%= TableWidth %>" summary="<%= Summary %>" class="<%= CssClass %>">
        <tr>
            <%#DisplayHeader()%>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <%#DisplayRepeater(Container)%>
    </ItemTemplate>
    <FooterTemplate></table></FooterTemplate>
</asp:Repeater>



The DisplayRepeater method -- one thing to mention here is that I am receiving the current Repeater item which is a DataRowView, but at no point am I associating it to any of the DataControlFields. I don't if this is simply implied as the current item in the Repeater's state or if it needs to be explicitly linked... if so, I don't see how this is done.

Protected Function DisplayRepeater(ByVal item As DataRowView) As String
	Dim tr As New TableRow

	CustomDataRepeater.Controls.Add(tr)

	If AutoGenerateColumns Then
		*** Some irrelevant code to the issue as this portion works fine ***
	Else
		Dim index As Integer = 0
		For Each c As DataControlField In Columns
		
		Dim cell As New DataControlFieldCell(c)
		c.InitializeCell(cell, DataControlCellType.DataCell, DataControlRowState.Normal, index)
		tr.Cells.Add(cell)
		index += 1
	END IF
	
	Return String.Empty
End Function



I was initially looping through the DataControlFieldCollection and manually evaluating the expressions and adding the result to a TableCell and everything worked except for the ItemTemplate (and for the fact that I would be reinventing the wheel by doing so). And I absolutely need the ItemTemplate to work. I did some googling and what little information I found, the DataControlFieldCell is what I was looking for and from my understanding would do the work of evaluating the variable and such. This is the part that is stumping (if the code in step 1 is correct obviously).

And the call to the custom control

<NS:DList runat="server" PageSize="20" GroupSize="10" AutoGenerateColumns="false" ID="dl1" CssClass="TableProps"                     ShowInactiveLinks="true">
	<Columns>
		<asp:BoundField runat="server" HeaderText="Name" DataField="Name" SortExpression="Name"  />
		<asp:HyperLinkField 
                        DataNavigateUrlFields="Name,Title" 
                        DataNavigateUrlFormatString="~/someotherfile.aspx?Name={0}&Title={1}" 
                        DataTextField="Title" 
                        HeaderText="Title"
                        SortExpression="Title" /> 
                <asp:BoundField runat="server" HeaderText="Office" DataField="Office" SortExpression="Office" />
                <asp:BoundField runat="server" HeaderText="Active" DataField="Active" />
                <asp:TemplateField runat="server" HeaderText="Active" SortExpression="Active">
                	<ItemTemplate>
                		<%#Tester.GetLocalizedIsActive(Eval("Active"))%>
                	</ItemTemplate>
                </asp:TemplateField>      
	</Columns>
</NS:DList>



Please let me know of any other information you may need to help out.

Thanks so much in advance,
André

This post has been edited by atognarelli: 22 July 2010 - 12:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using a DataControlFieldCollection in a Custom Control

#2 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Using a DataControlFieldCollection in a Custom Control

Posted 23 July 2010 - 02:14 PM

Think of the Repeater control as a "template" for the data that it is bound to.
That's pretty much all that it is: a template that dictates how the data is displayed.

I have no idea what you are trying to do in the DisplayRepeater method.
"Rows"/records are automatically created in the Repeater for each item in the data source that the repeater is bound to.

You do not need the DisplayRepeater method at all!
All you need to do is specify where the bound data should be placed and how it should be presented

Like this:

<asp:Repeater runat="server" ID="CustomDataRepeater">
    <HeaderTemplate>
        <table border="1" width="<%= TableWidth %>" summary="<%= Summary %>" class="<%= CssClass %>">
            <tr>
                <td colspan="2">
                    <%#DisplayHeader()%>
                </td>

            </tr>
            <tr><td>Widget ID</td><td>Description</td></tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "ID")%>
            </td>
            <td><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table></FooterTemplate>
</asp:Repeater>


Where in my server-side VB.NET code I would be binding to a private "Widgets" class...
Private widgets As List(Of Widget)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     If IsPostBack = False Then
        widgets = New List(Of Widget)
        For i As Integer = 0 To 9
            widgets.Add(New Widget(i, "Widget " + (i + 1).ToString))
        Next
        Session("widgets") = widgets
     Else
        widgets = Session("widgets")
     End If
     CustomDataRepeater.DataSource = widgets
     CustomDataRepeater.DataBind()
End Sub



Protected ReadOnly Property TableWidth() As String
    Get
       Return "50%"
   End Get
End Property
Protected ReadOnly Property Summary() As String
    Get
      Return "This table displays widgets!"
    End Get
End Property

Protected ReadOnly Property CssClass() As String
    Get
        Return ""
    End Get
End Property

Protected ReadOnly Property DisplayHeader() As String
   Get
       Return "Widgets Header"
   End Get
End Property

Private Class Widget
    Private _id As Integer
    Private _description As String
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property
    Public Sub New(ByVal id As Integer, ByVal description As String)
        Me._id = id
        Me._description = description
    End Sub
End Class


-Frinny
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1