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

Gridview does not load items properly with Ajax Manager

3 Answers 84 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JD Emmert
Top achievements
Rank 1
JD Emmert asked on 22 Feb 2011, 04:31 PM
Hi, I am trying to use RadAjaxManager to cause my gridview to add items when a button is clicked.  These items are stored in a session variable.  When I use the RadUpdatePanel, everything works as expected, except the whole page reloads and is slow.  I would like to show a loading panel over each contorl that is loading (my actual page is quite complex).  When I click the button to add an item to the radgrid, it shows the loading panel, but the grid does not show any new items.  However if I click other things on the page, the items that were added will appear.  I've been searching for quite some time for a solution to my problem.  Also note:I'm trying NOT to use javascript if possible.  My setup is as follows:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>
  
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
  
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  
    <telerik:RadScriptManager ID="scmAjax" runat="server" ScriptMode="Release" />
  
    <telerik:RadAjaxManager ID="radAjaxManager" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="radAjaxManager">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="rlpLoadingPanel"/>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadButton1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="rlpLoadingPanel"/>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="rlpLoadingPanel"/>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
  
  
    <telerik:RadButton ID="RadButton1" runat="server" Text="Assign" 
        onclick="RadButton1_Click" AutoPostBack="true">
    </telerik:RadButton>
  
    <telerik:RadButton ID="RadButton2" runat="server" Text="Request that will reload grid" 
        AutoPostBack="true">
    </telerik:RadButton>
  
    <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None">
    </telerik:RadGrid>
  
    <telerik:RadAjaxLoadingPanel ID="rlpLoadingPanel" runat="server" Skin="Default" IsSticky="false">
        </telerik:RadAjaxLoadingPanel>
  
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
  
namespace WebApplication5
{
    public struct Book
    {
        public string title
        {
            get;
            set;
        }
        public string author
       {
            get;
            set;
        }
    }
  
    public partial class _Default : System.Web.UI.Page
    {
        protected List<Book> GridViewBooks
        {
            get
            {
                return (List<Book>)Session["Books"];
            }
            set
            {
                Session["Books"] = value;
            }
        }
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Book> theBooks = new List<Book>();
                Book aBook = new Book();
                aBook.title = "First book title";
                aBook.author = "First book author";
                  
                theBooks.Add(aBook);
  
                GridViewBooks = theBooks;
                BindGrid();
            }
  
        }
  
        protected void RadButton1_Click(object sender, EventArgs e)
        {
            List<Book> theBooks = new List<Book>();
            Book aBook = new Book();
            aBook.title = "Another title " + DateTime.Now.ToString();
            aBook.author = "another book author";
  
            theBooks = GridViewBooks;
            theBooks.Add(aBook);
            GridViewBooks = theBooks;
  
            BindGrid();
        }
  
        private void BindGrid()
        {
            //bind gridview
            RadGrid1.DataSource = GridViewBooks;
            RadGrid1.DataBind();
        }
  
    }
}

Thanks for your help

3 Answers, 1 is accepted

Sort by
0
JD Emmert
Top achievements
Rank 1
answered on 22 Feb 2011, 05:35 PM
Also I forgot to mention that I've tried the NeedDataSource event and ReBind().  Also know as "advanced radgrid databinding."
0
JD Emmert
Top achievements
Rank 1
answered on 22 Feb 2011, 06:16 PM
Well, I found out that you can place the button inside of a panel with the radgrid and place in the RadManager that the panel updates the panel and it works.  However this is not a solution for me as in reality, the button is no where near the gridview.  Why does this work?  Shouldn't I be able to say "this button updates that gridview (plus other controls if needed)," no matter where they are on the page?
0
Radoslav
Telerik team
answered on 25 Feb 2011, 01:39 PM
Hi JD Emmert,

I am sending you a simple example which demonstrates the desired functionality. Into it I use the NeedDataSource event in order to bind the RadGrid:
void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GridViewBooks;
}

Into the Page_Load event one item is inserted into the GridViewBooks collection. This will guarantee that  when the page is loaded for the first time the grid is bound to one item:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
         List<Book> theBooks = new List<Book>();
         Book aBook = new Book();
         aBook.title = "First book title";
         aBook.author = "First book author";
         theBooks.Add(aBook);
         GridViewBooks = theBooks;
    }
}

Then on RadButton1_Click event handler a new item is inserted into the GridViewBooks collection and the RadGrid is rebound. Calling the Rebind() method will force the RadGrid to fire its NeedDataSource event.
protected void RadButton1_Click(object sender, EventArgs e)
{
    List<Book> theBooks = new List<Book>();
    Book aBook = new Book();
    aBook.title = "Another title " + DateTime.Now.ToString();
    aBook.author = "another book author";
     theBooks = GridViewBooks;
     theBooks.Add(aBook);
     GridViewBooks = theBooks;
 
     RadGrid1.Rebind();
}

I hope this helps.

All the best,
Radoslav
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
Grid
Asked by
JD Emmert
Top achievements
Rank 1
Answers by
JD Emmert
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or