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

Telerik ASP.NET Ajax RadGrid in MVC and ViewState?

1 Answer 180 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Levi
Top achievements
Rank 1
Levi asked on 15 Sep 2011, 01:18 AM
That's right, an ASP.NET Ajax control in MVC. I know I know, there are Telerik MVC controls but the Telerik MVC grid doesn't have everything I need so I'm dead set on using the ASP.NET Ajax control.

Anyways, I have the RadGrid up and running great. The problem occurs when I enable all the bells and whistles such as paging, sorting, filtering etc. It looks like it puts the group by/filter/sort data in an eventargument post variable:

__EVENTARGUMENT  FireCommand:2$RadGrid1$ctl00;GroupByColumn;Dialog

In my case, when the page reloads, nothing changes. Is there something I should enable for this to work in MVC? I have followed the instructions at http://www.telerik.com/help/aspnet-ajax/mvc-getting-started.html but this doesn't come up. The example in the previous url doesn't have all the bells and whistles, so I'm assuming there are additional steps I need to take to get this to work.

Thanks!

1 Answer, 1 is accepted

Sort by
0
Levi
Top achievements
Rank 1
answered on 16 Sep 2011, 01:02 PM

All ye who enter here, do not abandon hope. I was able to find a solution to this, though it's not very pretty. It uses a little reflection and hard coded mapping to tree of objects. Hopefully this will be a good starting point for anyone needing ViewState in MVC.

Partial View:

<%@ Control Language="C#" CodeBehind="RadGridExample.ascx.cs" Inherits="MvcApplication5.Views.Shared.RadGridExample" %>
<form runat="server">
 
    <telerik:RadScriptManager ID="scriptmanager2" runat="server"></telerik:RadScriptManager>
 
    <telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowMultiRowSelection="true" ShowGroupPanel="true" AllowFilteringByColumn="True" >
        <MasterTableView TableLayout="Fixed" >
            <Columns>
                <telerik:GridBoundColumn DataField="Name" HeaderText="Name" DataType="System.String" />
                <telerik:GridBoundColumn DataField="Age" HeaderText="Age" DataType="System.String" />
            </Columns>
        </MasterTableView>
        <ClientSettings AllowDragToGroup="true" AllowGroupExpandCollapse="false" >
        </ClientSettings>
    </telerik:RadGrid>
 
</form>

Code behind:
using System.Reflection;
using System.Web.Mvc;
using System.Web.UI;
using MvcApplication5.Models.ViewModels;
using Telerik.Web.UI;
 
namespace MvcApplication5.Views.Shared
{
    public class RadGridExample : ViewUserControl
    {
        protected void Page_Init()
        {
            RadGrid grid = this.Controls[0].FindControl("RadGrid1") as RadGrid;
 
            grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
 
            grid.DataBind();
 
            string viewState = Request.Form["__VIEWSTATE"];
 
            if (!string.IsNullOrEmpty(viewState))
            {
                LosFormatter formatter = new LosFormatter();
 
                object savedStateObject = formatter.Deserialize(viewState);
 
                MethodInfo method = grid.GetType().GetMethod("LoadViewState", BindingFlags.NonPublic | BindingFlags.Instance);
 
                // TODO: Find a less brittle/more elegant way to isolate the appropiate viewstate object for this control
                // In the case of Telerik's RadGrid, the key wasy find the tree that had an array of 13 objects
                method.Invoke(grid, new object[] { (((((((((savedStateObject as Pair).First as Pair).Second as Pair).Second as System.Collections.ArrayList)[1] as Pair).Second as System.Collections.ArrayList)[1] as Pair).Second as System.Collections.ArrayList)[1] as Pair).First });
            }
 
            string eventArgument = Request.Form["__EVENTARGUMENT"];
 
            if (!string.IsNullOrEmpty(eventArgument))
            {
                grid.RaisePostBackEvent(eventArgument);
            }
        }
 
        void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            (sender as RadGrid).DataSource = this.Model as HomeIndexViewModel;
        }
    }
}

Tags
Grid
Asked by
Levi
Top achievements
Rank 1
Answers by
Levi
Top achievements
Rank 1
Share this question
or