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

Dynamic Grid and Postback

1 Answer 214 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff Ballard
Top achievements
Rank 1
Jeff Ballard asked on 16 Apr 2009, 06:42 PM
I am trying to figure out how to do what I need and seem to find examples for all the various aspects (save one) but can't pull it all together.  Here's what I am trying to do:

  1. Programmatically build my grid with GridTemplateColumns (radio button lists, text boxes and dropdowns).  I've used these as an example: http://www.telerik.com/help/aspnet/grid/grdprogrammaticcreation.html.  I have no issue with creating the template columns - my columns get created.
  2. Default the grid to edit mode at all times.  I used this example: http://www.telerik.com/help/aspnet/grid/grddefaulteditmodeforgriditemsoninitialload.html
  3. The last item, and this is the part I haven't found an example for, is to have a button that is not a part of the grid or its associated ItemCommand that posts back and from that event get the changed items. Is this even possible?  The grid is really part of a larger form that needs to get saved when the save button is clicked.

What we are doing is creating a form/questionnaire for users to fill out that is dynamically built based on metadata from the database, hence everything being done programatically.

The problem I am running into occurs after page is posted back.  When the page posts back, the grid is set to nothing (this is VB) until it gets recreated in code.  After I define the grid structure in the PreInit event the Page Load event fires (I don't rebind at this point, I don't want to overwrite any changed values), but I can't see my edited values.  I thought that the grid would be loaded from view state by the time the page load event is done, but no luck.  Any ideas why I'm not seeing it?  My code is below.

Thanks,

Jeff

Imports Telerik.Web.UI 
 
Partial Public Class _Default 
    Inherits System.Web.UI.Page 
 
    Protected WithEvents rgOne As RadGrid 
 
    Private Sub Page_Init(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Init 
        DefineGridStructure() 
    End Sub 
 
    Private Sub rgOne_NeedDataSource(ByVal sender As ObjectByVal e As GridNeedDataSourceEventArgs) Handles rgOne.NeedDataSource 
 
        rgOne = CType(sender, RadGrid) 
 
        Dim newTable As DataTable = New DataTable() 
 
        Dim dc1 As New DataColumn 
 
        dc1.ColumnName = "ColumnA" 
        dc1.DataType = Type.GetType("System.String"
        newTable.Columns.Add(dc1) 
 
        Dim row1 As DataRow = newTable.NewRow() 
        row1.Item("ColumnA") = "Row 1 Value, Column A" 
 
        Dim row2 As DataRow = newTable.NewRow() 
        row2.Item("ColumnA") = "Row 2 Value, Column A" 
 
        newTable.Rows.Add(row1) 
        newTable.Rows.Add(row2) 
 
        rgOne.DataSource = newTable 
 
    End Sub 
 
    Private Sub DefineGridStructure() 
 
        rgOne = New RadGrid() 
 
        rgOne.AllowMultiRowEdit = True 
        rgOne.AutoGenerateColumns = False 
        rgOne.AutoGenerateEditColumn = False 
        rgOne.MasterTableView.EnableColumnsViewState = True 
        rgOne.EnableViewState = True 
 
        Dim templateBox As New TemplateTextBox("textOne"
        Dim templateColumn As New GridTemplateColumn() 
        templateColumn.EditItemTemplate = templateBox 
        templateColumn.DataField = "ColumnA" 
        templateColumn.HeaderText = "Column A" 
 
        rgOne.Columns.Add(templateColumn) 
 
        ph1.Controls.Add(rgOne) 
 
    End Sub 
 
    Private Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load 
        If Not Page.IsPostBack Then 
            rgOne.Rebind() 
        Else 
            For Each editedItem As GridEditableItem In rgOne.EditItems 
                Dim newValues As New Hashtable() 
                rgOne.MasterTableView.ExtractValuesFromItem(newValues, editedItem) 
            Next 
        End If 
    End Sub 
 
    Private Sub Page_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.PreRender 
        For Each dataItem As GridDataItem In rgOne.Items 
            dataItem.Edit = True 
        Next 
        rgOne.Rebind() 
    End Sub 
End Class 
 
Class TemplateTextBox 
    Implements IBindableTemplate 
 
    Private m_CtrlName As String 
 
    Public Sub New(ByVal ctrlName As String
        MyBase.New() 
        m_CtrlName = ctrlName 
    End Sub 
 
    Public Function ExtractValues(ByVal container As System.Web.UI.Control) As System.Collections.Specialized.IOrderedDictionary Implements System.Web.UI.IBindableTemplate.ExtractValues 
        Dim od As New OrderedDictionary 
        Return od 
    End Function 
 
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn 
        Dim box As New RadTextBox() 
        box.ID = m_CtrlName 
        container.Controls.Add(box) 
    End Sub 
End Class 
 

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 22 Apr 2009, 10:22 AM
Hi Jeff,

Look at the following article for more information about how to force several grid items in edit mode and then update them on single button click.

Additionnally, I suggest that you try to change your code as it is shown bellow:
VB:
Imports Telerik.Web.UI    
    
Partial Public Class _Default    
    Inherits System.Web.UI.Page    
    
    Protected WithEvents rgOne As RadGrid    
    
    Private Sub Page_Init(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Init    
        DefineGridStructure()    
    End Sub    
    
    Private Sub rgOne_NeedDataSource(ByVal sender As ObjectByVal e As GridNeedDataSourceEventArgs) Handles rgOne.NeedDataSource    
    
        rgOne = CType(sender, RadGrid)    
    
        Dim newTable As DataTable = New DataTable()    
    
        Dim dc1 As New DataColumn    
    
        dc1.ColumnName = "ColumnA"    
        dc1.DataType = Type.GetType("System.String")    
        newTable.Columns.Add(dc1)    
    
        Dim row1 As DataRow = newTable.NewRow()    
        row1.Item("ColumnA") = "Row 1 Value, Column A"    
    
        Dim row2 As DataRow = newTable.NewRow()    
        row2.Item("ColumnA") = "Row 2 Value, Column A"    
    
        newTable.Rows.Add(row1)    
        newTable.Rows.Add(row2)    
    
        rgOne.DataSource = newTable    
    
    End Sub    
    
    Private Sub DefineGridStructure()    
    
        rgOne = New RadGrid()    
    
        rgOne.AllowMultiRowEdit = True    
        rgOne.AutoGenerateColumns = False    
        rgOne.AutoGenerateEditColumn = False    
        rgOne.MasterTableView.EnableColumnsViewState = True    
        rgOne.EnableViewState = True    
    
        Dim templateBox As New TemplateTextBox("textOne")    
        Dim templateColumn As New GridTemplateColumn()    
        templateColumn.EditItemTemplate = templateBox    
        templateColumn.DataField = "ColumnA"    
        templateColumn.HeaderText = "Column A"    
    
        rgOne.Columns.Add(templateColumn)    
    
        ph1.Controls.Add(rgOne)    
    
    End Sub    
    
  Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemCreated  
         If (Not Page.IsPostBack AndAlso TypeOf e.Item Is GridEditableItem) Then 
            e.Item.Edit = True 
         End If 
    End Sub 
 
End Class    
    
Class TemplateTextBox    
    Implements IBindableTemplate    
    
    Private m_CtrlName As String    
    
    Public Sub New(ByVal ctrlName As String)    
        MyBase.New()    
        m_CtrlName = ctrlName    
    End Sub    
    
    Public Function ExtractValues(ByVal container As System.Web.UI.Control) As System.Collections.Specialized.IOrderedDictionary Implements System.Web.UI.IBindableTemplate.ExtractValues    
        Dim od As New OrderedDictionary    
        Return od    
    End Function    
    
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn    
        Dim box As New RadTextBox()    
        box.ID = m_CtrlName    
        container.Controls.Add(box)    
    End Sub    
End Class 

Kind regards,
Pavlina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Jeff Ballard
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or