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:
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
- 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.
- Default the grid to edit mode at all times. I used this example: http://www.telerik.com/help/aspnet/grid/grddefaulteditmodeforgriditemsoninitialload.html
- 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 Object, ByVal e As System.EventArgs) Handles Me.Init |
DefineGridStructure() |
End Sub |
Private Sub rgOne_NeedDataSource(ByVal sender As Object, ByVal 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 Object, ByVal 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 Object, ByVal 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 |