This is a migrated thread and some comments may be shown as answers.

Dynamic Grids & NeedDataSource - grid disappearing

4 Answers 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Karl Wilkens
Top achievements
Rank 1
Karl Wilkens asked on 06 Jan 2011, 11:39 AM
Hi,

We are dynamically building radgrids in PageLoad and adding an event handler for needDataSource as follows

            Dim NewGrid As New RadGrid
                NewGrid.DataSource = dv
                NewGrid.Skin = "Web20"
                NewGrid.ID = "rg_" & FormID.ToString
                NewGrid.AllowFilteringByColumn = True
                NewGrid.AllowSorting = True
                NewGrid.AllowPaging = True
               AddHandler NewGrid.NeedDataSource, AddressOf Me.grid_NeedDataSource
                NewGrid.DataBind()

On any sort or filter operations on the resulting grid, the grid never calls the grid_NeedDataSource which is as follows

 Protected Sub grid_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
        Dim FormID As Integer
        Dim dv As New DataView

        Try
            Dim rg As RadGrid = CType(source, Telerik.Web.UI.RadGrid)
            If rg IsNot Nothing Then

                FormID = CInt(rg.ID.ToString.Remove(0, 3))
                dv = CreateDataTable(FormID)
                rg.DataSource = dv
            End If

        Catch ex As Exception

        End Try
       
    End Sub

I assume this has to do with the page and control life cycle - I have tried creating the grids in PageLoad (not in an IsPostback test) and this simply reloads the grid but without sort/filtering. Can anyone tell me what I am doing wrong here?

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Jan 2011, 02:06 PM
Hello Karl,

You should never call Rebind() method in NeedDataSource event handler or DataBind() for the grid at any stage of the page lifecycle.

Hope this information helps,
Princy.
0
Karl Wilkens
Top achievements
Rank 1
answered on 06 Jan 2011, 03:00 PM
Hi,

You lost me on this. In the example we are not rebinding in NeedDataSource - simply pointing to the datasource but no bind. In anycase, NeedDataSource never executes and I am not sure why.
0
Pavlina
Telerik team
answered on 06 Jan 2011, 03:41 PM
Hi Karl,

Please find attached to this message a simple working project which demonstrates how you can achieve the desired functionality.

Additionally, I recommend that you review the help articles below:
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html
http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html

Best wishes,
Pavlina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Jack
Top achievements
Rank 1
answered on 14 Sep 2011, 12:46 AM
Just a couple of extra suggestions.  Dynamic instantiation of a control often occurs in a click event delegate.  ASP.NET click events come in after Page_Load so if you are trying to instantiate Rad Controls after a click event there are a couple things to keep in mind. 
  1. First, you are going to have to manually call Rebind because you are past the Page Load phase
  2. Click events happen as part of a PostBack so instantiating during a post back means you will have to initialize your Rad control in the click event handler as if this is not a PostBack.
Tags
Grid
Asked by
Karl Wilkens
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Karl Wilkens
Top achievements
Rank 1
Pavlina
Telerik team
Jack
Top achievements
Rank 1
Share this question
or