all groups > asp.net building controls > june 2006 >
You're in the

asp.net building controls

group:

Custom Datagrid Implementation


Custom Datagrid Implementation Brent Ritchie
6/28/2006 8:47:19 AM
asp.net building controls: Hello,

I have been given the assignment of creating a custom datagrid for
our company. The specs are pretty simple: 1) Use a webservice to
populate and do other processing tasks.

So far I made a test web service and derived it from a common
interface. The Custom datagrid uses this interface to call methods of
the webservice. This is ok. The problem I have is that when the
datagrid is created, I cannot create any of the dynamic controls until
I bind to the data and I cannot bind to the data until after the
control has been created.

Given this, how would I go about setting the viewstate / postdata
manually on all of these controls.
Re: Custom Datagrid Implementation (Code Listing Long) Brent Ritchie
6/28/2006 12:35:35 PM
I've decided to post the source of what I have now. I don't think that
my previous description was clear after reading it again.

Imports System.Text
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports IWebServices

<ToolboxData("<{0}:FLSDataGrid runat=server></{0}:FLSDataGrid>")> _
Public Class FLSDataGrid
Inherits System.Web.UI.WebControls.DataGrid

#Region "Public Methods"

#Region "Constructors"

Public Sub New()

MyBase.New()
Me.EnableViewState = True

m_addrow = New DataGridItem(-1, -1, ListItemType.SelectedItem)

Me.ViewState.Add("m_SortCriteria", "")
Me.ViewState.Add("m_SortDirection", "ASC")
Me.ViewState.Add("m_SearchEnabled", False)

End Sub

#End Region

#End Region

#Region "Public Properties"

Public Property WebDataSource() As IDataWebService
Get
Return m_WebDataSource
End Get
Set(ByVal Value As IDataWebService)
m_WebDataSource = Value
MyBase.VirtualItemCount =
m_WebDataSource.RetrieveRowCount()
Me.PageEvent(Me, New DataGridPageChangedEventArgs(Me,
Me.CurrentPageIndex))
End Set
End Property

Private Property TextFieldsNeeded()
Get
If Not Me.ViewState.Item("m_TextFields") = Nothing Then
Return Me.ViewState.Item("m_TextFields")
Else
Return 0
End If
End Get
Set(ByVal Value)
Me.ViewState.Item("m_TextFields") = Value
End Set
End Property

Public Property SearchEnabled() As Boolean
Get
Return Me.ViewState.Item("m_SearchEnabled")
End Get
Set(ByVal Value As Boolean)
Me.ViewState.Item("m_SearchEnabled") = Value
End Set
End Property

Public Property SortCriteria() As String
Get
Return Me.ViewState.Item("m_SortCriteria")
End Get
Set(ByVal Value As String)
Me.ViewState.Item("m_SortCriteria") = Value
End Set
End Property

Public Property SortDirection() As String
Get
Return Me.ViewState.Item("m_SortDirection")
End Get
Set(ByVal Value As String)
Me.ViewState.Item("m_SortDirection") = Value
End Set
End Property

#End Region

#Region "Protected Methods"

Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)

MyBase.Render(output)

End Sub

Protected Friend Overloads Sub OnPreRender(ByVal e As EventArgs)
For Each c As Label In MyBase.Controls
c.EnableViewState = False
Next
Me.CreateAndRegisterScripts()
Me.CreateFooter()
End Sub

Protected Sub Sort(ByVal sender As Object, ByVal e As
DataGridSortCommandEventArgs) Handles MyBase.SortCommand

If SortCriteria = e.SortExpression Then
If SortDirection = "ASC" Then
SortDirection = "DESC"
Else
SortDirection = "ASC"
End If
Else
SortCriteria = e.SortExpression
SortDirection = "DESC"
End If

Me.PageEvent(Me, New DataGridPageChangedEventArgs(Me,
Me.CurrentPageIndex))

End Sub

Protected Overloads Overrides Sub LoadViewState(ByVal savedState As
Object)

MyBase.LoadViewState(savedState)

For Each str As String In Me.ViewState.Keys
If str.StartsWith("txtAddRow") Then
Page.Response.Write(Me.ViewState.Item(str))
End If
Next
Me.CreateFooter()

End Sub

Protected Sub PageEvent(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs) Handles MyBase.PageIndexChanged

MyBase.CurrentPageIndex = e.NewPageIndex
If SearchEnabled = True Then

Dim param As DataSet = New DataSet("Search Parameters")
param.Tables.Add("Parameters")

param.Tables("Parameters").Rows.Add(param.Tables("Parameters").NewRow())

Dim editcolumn As Boolean = True
Dim ii As Integer = 0

For Each tc As TableCell In m_addrow.Cells

If editcolumn Then

editcolumn = False

Else

param.Tables("Parameters").Columns.Add("@" +
DirectCast(MyBase.DataSource,
DataSet).Tables(0).Columns(ii).ToString())
param.Tables("Parameters").Rows(0).Item(ii) =
Me.ViewState.Item(DirectCast(tc.Controls(0), TextBox).ID).ToString()
ii += 1

End If

Next

MyBase.DataSource = WebDataSource.RetrieveRecord(param)

Else

MyBase.DataSource =
WebDataSource.RetrieveRowsSubset(e.NewPageIndex, MyBase.PageSize,
Me.SortCriteria, Me.SortDirection)

End If

MyBase.DataBind()
Me.CreateFooter()

End Sub

Protected Sub EditEvent(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs) Handles MyBase.EditCommand

Me.EditItemIndex = e.Item.ItemIndex
Me.PageEvent(Me, New DataGridPageChangedEventArgs(Me,
Me.CurrentPageIndex))

End Sub

Protected Sub CancelEvent(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs) Handles MyBase.CancelCommand

Me.EditItemIndex = -1
Me.PageEvent(Me, New DataGridPageChangedEventArgs(Me,
Me.CurrentPageIndex))

End Sub

Protected Sub UpdateEvent(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs) Handles MyBase.UpdateCommand

Me.PageEvent(Me, New DataGridPageChangedEventArgs(Me,
Me.CurrentPageIndex))

Dim param As DataSet = New DataSet("Update Parameters")
param.Tables.Add("Parameters")

param.Tables("Parameters").Rows.Add(param.Tables("Parameters").NewRow())

Dim editcolumn As Boolean = True
Dim deletecolumn As Boolean = True
Dim i As Integer = 0

For Each tc As TableCell In e.Item.Cells
If editcolumn Then
editcolumn = False
ElseIf deletecolumn Then
deletecolumn = False
Else

Dim tb As TextBox = tc.Controls(0)
param.Tables("Parameters").Columns.Add("@" +
DirectCast(MyBase.DataSource, DataSet).Tables(0).Columns(i).ToString())
param.Tables("Parameters").Rows(0).Item(i) = tb.Text
i += 1

End If
Next

Me.WebDataSource.UpdateRecord(param)
Me.EditItemIndex = -1
Me.PageEvent(Me, New DataGridPageChangedEventArgs(Me,
Me.CurrentPageIndex))

End Sub

Protected Sub DeleteEvent(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs) Handles MyBase.DeleteCommand

Re: Custom Datagrid Implementation (Code Listing Long) Brent Ritchie
6/29/2006 8:09:56 AM

[quoted text, click to view]

After Some quick changes for testing. This is exactly what I needed.
Just to be clear though, CreateControlHierarchy() happens after the
binding but just before rendering? I've been looking through the
Lifecycle examples and haven't seen this before.

The reason I asked about viewstate is that the controls are being
created so late that they don't have a chance for there viewstate or
postback data to be restored automatically. I needed viewstate to be
available after the controls are created to manually repopulate them.

Thanks, this was what I needed.
Re: Custom Datagrid Implementation (Code Listing Long) Alessandro Zifiglio
6/29/2006 11:37:22 AM
hi Brent, try overriding CreateControlHierarchy method of your base datagrid
control. eg.

Protected Overrides Sub CreateControlHierarchy ( _
useDataSource As Boolean _
)

MyBase.CreateControlHierarchy(useDataSource) ' call the base method.
' now that the control hierarchy that is used
' to render the DataGrid is created, lets start creating our custom
controls.
' Since the controls have already been created via our
' call to MyBase.CreateControlHierarchy(), you'd be looping
' through each row of the rendered datagrid to add
' your custom controls. Is this what you were after ?
' Further you asked about viewstate so :
If (useDataSource) Then
' this means there is nothing in viewstate,
' since we are rendering the first time.
Else
' no need to get data from the underlying datasource since
' we are going to be rendering from viewstate, probably a postback scenario
End if
End Sub

Alessandro Zifiglio

"Brent Ritchie" <ritchie.brent@gmail.com> ha scritto nel messaggio
news:1151523335.337307.110910@d56g2000cwd.googlegroups.com...
[quoted text, click to view]
Re: Custom Datagrid Implementation (Code Listing Long) Alessandro Zifiglio
6/29/2006 5:53:25 PM
Brent, CreateControlHierarchy() is a method exposed by the datagrid, which
is actually called from CreateChildControls() method with an argument of
false, when its the first time the control is rendering. For all other
times, CreateControlHierachy() is called with an argument of true from
within the OnDataBinding method, so populating the data from the underlying
datasource itself and not viewstate.

Glad that helped you.
Have a good day,
Alessandro Zifiglio
"Brent Ritchie" <ritchie.brent@gmail.com> ha scritto nel messaggio
news:1151593795.659798.133710@y41g2000cwy.googlegroups.com...
[quoted text, click to view]

Re: Custom Datagrid Implementation (Code Listing Long) Alessandro Zifiglio
6/29/2006 6:05:56 PM
oops :X
I mean the exact opposite of what i stated earlier, so to avoid confusion
this is exactly what i was trying to state :
CreateControlHierarchy is called from within the CreateChildControls method
with an argument of false when the control understands that it needs to
populate the datacontrol from viewstate and not from the underlying
datasource.

while otherwise, CreateControlHierarchy() is called with an argument of true
from onDataBinding() method and gets the data from the underlying
datasource, which is triggered when the DataBind() method on your
datacontrol is called.

Apologies for confusion.
Regards,
Alessandro Zifiglio

"Alessandro Zifiglio" <AlessandroZifiglio @ -h-o-t-m-a-i-l-c-o-m> ha scritto
nel messaggio news:O2pmnQ5mGHA.976@TK2MSFTNGP03.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button