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

Save Grid Settings Render call fails

5 Answers 52 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
mac
Top achievements
Rank 1
mac asked on 01 Oct 2009, 10:09 PM
hey.. I am attempting to get my grid to save their custom grouped setting for each of my users via the asp.net profile objects. this works for me when i am not using the ajax manager. (I am using the grid settings persistor sample for the guidelines. )
My question is this.. how to save the grid settings when the ajax call avoids the Render Sub.. I hae tried the goups changing but found that really ugly as you would have to apply it on each grid in the page.  ( it also blew up each time saying the object no longer existed if you moved an item out)
If you have any ideas I would appreciate hearing them
.class file
Imports Microsoft.VisualBasic  
'Namespace WebApplication1  
Imports System  
Imports System.Collections  
Imports System.IO  
Imports System.Web.UI  
Imports System.Web.UI.WebControls  
Imports Telerik.Web.UI  
 
 
Public Class GridSettingsPersister  
 
    Private gridInstance As RadGrid  
 
    Public Sub New(ByVal gridInstance As RadGrid)  
        MyBase.New()  
        Me.gridInstance = gridInstance  
    End Sub  
 
    'this method should be called on Render  
    Public Function SaveSettings() As String  
        Dim gridSettings() As Object = New Object((4) - 1) {}  
        'Save groupBy  
        Dim groupByExpressions As GridGroupByExpressionCollection = gridInstance.MasterTableView.GroupByExpressions  
        Dim groupExpressions() As Object = New Object((groupByExpressions.Count) - 1) {}  
        Dim count As Integer = 0 
        For Each expression As GridGroupByExpression In groupByExpressions  
            groupExpressions(count) = CType(expression, IStateManager).SaveViewState  
            count = (count + 1)  
        Next  
        gridSettings(0) = groupExpressions  
        'Save sort expressions  
        gridSettings(1) = CType(gridInstance.MasterTableView.SortExpressions, IStateManager).SaveViewState  
        'Save columns order  
        Dim columnsLength As Integer = (gridInstance.MasterTableView.Columns.Count + gridInstance.MasterTableView.AutoGeneratedColumns.Length)  
        Dim columnOrder() As Pair = New Pair(columnsLength - 1) {}  
 
        Dim allColumns As ArrayList = New ArrayList(columnsLength)  
        allColumns.AddRange(gridInstance.MasterTableView.Columns)  
        allColumns.AddRange(gridInstance.MasterTableView.AutoGeneratedColumns)  
        Dim i As Integer = 0 
        For Each column As GridColumn In allColumns  
            Dim p As Pair = New Pair  
            p.First = column.OrderIndex  
            p.Second = column.HeaderStyle.Width  
            columnOrder(i) = p  
            i = (i + 1)  
        Next  
        gridSettings(2) = columnOrder  
        'Save filter expression  
        gridSettings(3) = CType(gridInstance.MasterTableView.FilterExpression, Object)  
 
        Dim formatter As LosFormatter = New LosFormatter  
        Dim writer As StringWriter = New StringWriter  
        formatter.Serialize(writer, gridSettings)  
        Return writer.ToString  
    End Function  
 
    'this method should be called on PageInit  
    Public Sub LoadSettings(ByVal settings As String)  
        Dim formatter As LosFormatter = New LosFormatter  
        Dim reader As StringReader = New StringReader(settings)  
        Dim gridSettings() As Object = CType(formatter.Deserialize(reader), Object())  
        'Load groupBy  
        Dim groupByExpressions As GridGroupByExpressionCollection = Me.gridInstance.MasterTableView.GroupByExpressions  
        groupByExpressions.Clear()  
        Dim groupExpressionsState() As Object = CType(gridSettings(0), Object())  
        Dim count As Integer = 0 
        For Each obj As Object In groupExpressionsState  
            Dim expression As GridGroupByExpression = New GridGroupByExpression  
            CType(expression, IStateManager).LoadViewState(obj)  
            groupByExpressions.Add(expression)  
            count = (count + 1)  
        Next  
        'Load sort expressions  
        Me.gridInstance.MasterTableView.SortExpressions.Clear()  
        CType(Me.gridInstance.MasterTableView.SortExpressions, IStateManager).LoadViewState(gridSettings(1))  
        'Load columns order  
        Dim columnsLength As Integer = (Me.gridInstance.MasterTableView.Columns.Count + Me.gridInstance.MasterTableView.AutoGeneratedColumns.Length)  
        Dim columnOrder() As Pair = CType(gridSettings(2), Pair())  
        If (columnsLength = columnOrder.Length) Then  
            Dim allColumns As ArrayList = New ArrayList(columnsLength)  
            allColumns.AddRange(Me.gridInstance.MasterTableView.Columns)  
            allColumns.AddRange(Me.gridInstance.MasterTableView.AutoGeneratedColumns)  
            Dim i As Integer = 0 
            For Each column As GridColumn In allColumns  
                column.OrderIndex = CType(columnOrder(i).First, Integer)  
                column.HeaderStyle.Width = CType(columnOrder(i).Second, Unit)  
                i = (i + 1)  
            Next  
        End If  
        'Load filter expression  
        Me.gridInstance.MasterTableView.FilterExpression = CType(gridSettings(3), String)  
    End Sub  
 
 
 
 
End Class  
 
<Serializable()> Public Class Pet  
    Public Sub New()  
        ' default ctor required for serializer   
    End Sub  
 
    Private _names As New Hashtable()  
    Public Property Names() As Hashtable  
        Get  
            Return _names  
        End Get  
        Set(ByVal value As Hashtable)  
            _names = value 
        End Set  
    End Property  
 
 
    Public Sub SavePetNames(ByVal Grid As RadGrid, ByVal GridName As String)  
        Dim settings As New GridSettingsPersister(Grid)  
        Dim settingvalue As String = settings.SaveSettings()  
        _names(GridName) = settingvalue  
        '   _names.Add(GridName, settingvalue)  
    End Sub  
 
 
 
End Class  
 
 
 
 
 
 
'End Namespace 

.ascx file
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init  
        '   export.Visible = True 
        For Each c As Control In Me.Controls  
            If TypeOf c Is RadGrid Then  
                Dim settings As New GridSettingsPersister(c)  
                If Profile.Pet.Names.ContainsKey(functions.getunique(c)) Then  
                    settings.LoadSettings(Profile.Pet.Names(functions.getunique(c)).ToString())  
                End If  
            End If  
        Next  
 
    End Sub  
 
 
    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)  
        MyBase.Render(writer)  
        For Each c As Control In Me.Controls  
            If TypeOf c Is RadGrid Then  
                Profile.Pet.SavePetNames(c, functions.getunique(c))  
            End If  
        Next  
    End Sub  
 
 
Protected Sub RadGrid1_GroupsChanging(ByVal source As Object, ByVal e As Telerik.Web.UI.GridGroupsChangingEventArgs) Handles RadGrid1.GroupsChanging  
'This doesn't work    
    '   Profile.Pet.SavePetNames(RadGrid1, functions.getunique(RadGrid1))  
 
    End Sub  
 
 

source
<telerik:RadAjaxManager   ID="RadAjaxManager1" runat="server">  
    <AjaxSettings> 
        <telerik:AjaxSetting AjaxControlID="RadGrid1">  
            <UpdatedControls> 
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
            </UpdatedControls> 
        </telerik:AjaxSetting> 
</telerik:RadAjaxManager> 

5 Answers, 1 is accepted

Sort by
0
Danny Scheelings
Top achievements
Rank 1
answered on 05 Oct 2009, 10:27 AM
I have exactly the same problem. Does anybody have a solution?
Maybe another event of the page/usercontrol which we can hook up to? Or an event of the RadAjaxManager?

Plz help !!!

Thx,
Dannu
0
Danny Scheelings
Top achievements
Rank 1
answered on 05 Oct 2009, 10:43 AM
I just tried saving the settings at the OnPreRender method and it looks like this is the solution. Any ideas if there could be a problem with this?

Thx,
Danny
0
Tsvetoslav
Telerik team
answered on 07 Oct 2009, 06:24 AM
Hi Danny,
Hello Mac,

The Render event of the page is always called no matter if an ajax request or a regular post back occurs. Our code library example (here) also uses RadAjaxManager to ajaxify the grids. Am I missing something?

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
mac
Top achievements
Rank 1
answered on 07 Oct 2009, 05:29 PM
Hey Tsvetoslav,
the problem seemed to start erupting when I moved the grids into their own user controls. ie somegrid.ascx
if you have the overrires prerender in the ascx and wrapped in an ajax panel or similar, the event won't fire other than the initial load. I checked Danny's solution and it does work, you still need to have the ovverrides event and savesettings. Anyway, from the way I can recall this was the issue. The problem presents when posting back from a usercontrol
webusercontrol.ascx.vb code
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init  
        For Each c As Control In pnl.Controls  
            If TypeOf c Is RadGrid Then  
 
                Dim settings As New GridSettingsPersister(c)  
    if Profile.Pet.Names.ContainsKey(getunique(c)) Then  
    settings.LoadSettings(Profile.Pet.Names(getunique(c)).ToString())  
    End If  
 
    End If  
    Next  
End Sub  
 
    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)  
        MyBase.Render(writer)  ' this will only fire the first time
    End Sub  
 
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender  
            For Each c As Control In pnl.Controls   'me.controls fails  
            If TypeOf c Is RadGrid Then  
                Profile.Pet.SavePetNames(c, getunique(c))  
            End If  
        Next  
    End Sub 
webusercontrol.ascx markup
 <telerik:RadAjaxPanel ID="pnl" runat="server">  
                      <telerik:RadGrid  ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"   
              GridLines="None" ShowGroupPanel="True" AutoGenerateColumns="False">  
                  <HeaderContextMenu> 
                      <CollapseAnimation Duration="200" Type="OutQuint" /> 
                  </HeaderContextMenu> 
                  <MasterTableView DataKeyNames="ID" DataSourceID="SqlDataSource1" > 
                      <Columns> 
                          <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" HeaderText="ID" ReadOnly="True" SortExpression="ID" 
                              UniqueName="ID">  
                          </telerik:GridBoundColumn> 
                          <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name">  
                          </telerik:GridBoundColumn> 
                          <telerik:GridBoundColumn DataField="AccountNo" HeaderText="AccountNo" SortExpression="AccountNo" UniqueName="AccountNo">  
                          </telerik:GridBoundColumn> 
                          <telerik:GridBoundColumn DataField="Balance" DataType="System.Decimal" HeaderText="Balance" SortExpression="Balance" 
                              UniqueName="Balance">  
                          </telerik:GridBoundColumn> 
                          <telerik:GridBoundColumn DataField="InsertDate" DataType="System.DateTime" HeaderText="InsertDate" SortExpression="InsertDate" 
                              UniqueName="InsertDate">  
                          </telerik:GridBoundColumn> 
                      </Columns> 
                  </MasterTableView> 
                  <ClientSettings AllowDragToGroup="True">  
                  </ClientSettings> 
                  <FilterMenu> 
                      <CollapseAnimation Duration="200" Type="OutQuint" /> 
                  </FilterMenu> 
              </telerik:RadGrid> 
            </telerik:RadAjaxPanel> 
and finally page.aspx

<%

@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

 

 

<uc1:WebUserControl ID="WebUserControl1" runat="server" />

 

0
Tsvetoslav
Telerik team
answered on 09 Oct 2009, 10:45 AM
Hi mac,

You can still use the Page's Render event, not the user controls'. You just need to get a reference of the user control, find the grid in it and load the settings from the profile.

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Ajax
Asked by
mac
Top achievements
Rank 1
Answers by
Danny Scheelings
Top achievements
Rank 1
Tsvetoslav
Telerik team
mac
Top achievements
Rank 1
Share this question
or