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

[Solved] Dynamically generating RadGrid on user action

2 Answers 141 Views
Grid
This is a migrated thread and some comments may be shown as answers.
cheburek
Top achievements
Rank 1
cheburek asked on 18 May 2009, 04:15 PM
I have dynamically generated user control that contains RadGrid.
Protected Overrides Sub CreateChildControls() 
      MyBase.CreateChildControls() 
      GenerateTable() 
    End Sub 
 
    Private Sub GenerateTable() 
      If BtnExpandOpen Then 
        Dim ctrl = GetProperControl(RowIndex) 
        If ctrl IsNot Nothing Then 
          ctrl.WriteToDisplay(Me
          PanelCategory.Controls.Add(ctrl) 
        End If 
      End If 
    End Sub 
 
    Private Sub btnExpand_Click(ByVal sender As ObjectByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExpand.Click 
      BtnExpandOpen = Not BtnExpandOpen 
      SetExpandButtonState() 
 
      GenerateTable() 
    End Sub 

This control I need to show up due to user request (some button click). When user clicks on button control is rendered properly but if I try to change page then grid is not changing its state.
If i try to change the page size then it throws exception like that:
[NullReferenceException: Object reference not set to an instance of an object.] 
   Telerik.Web.UI.RadGrid.RaisePostBackEvent(String eventArgument) +8609 
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175 
 
What have I do if I need to create RagGrid not in PageInit or PageLoad events but rendered grid must have some postbacks (i.e. paging)?
Thank you

2 Answers, 1 is accepted

Sort by
0
cheburek
Top achievements
Rank 1
answered on 19 May 2009, 11:25 AM
If you try creating your controls in the CreateChildControls you have to get the old value somehow.
The typical solution is to store this value in a property backed in the ViewState.
    Protected Overrides Sub LoadViewState(ByVal savedState As Object
      MyBase.LoadViewState(TryCast(savedState, Pair).First) 
      EnsureChildControls() 
    End Sub 
 
    Protected Overrides Function SaveViewState() As Object 
      Return New Pair(MyBase.SaveViewState(), Nothing
    End Function 

0
Iana Tsolova
Telerik team
answered on 21 May 2009, 12:33 PM
Hi cheburek,

Note that if grid is created entirely i code behind, it should be created on Page_Init as described here.
I suggest that you check out this article as see how grid structure could be changed dynamically on Page_Init.

Give it a try and let me know how it goes.

All the best,
Iana
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
cheburek
Top achievements
Rank 1
Answers by
cheburek
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or