I am trying to create a webgrid control that can use AJAX to do paging back and forth. I created the webgrid on an ASPX page and bound it to a dataset and then enabled AJAX and it pages OK using AJAX to populate the pages.
I then tried to do the same thing generating the webgrid control in code behind, setting the properties of the webgrid to be the same as the properties that I found in the source of the ASPX page. When I do this, the grid shows up OK when you load the page, but it doesn't page when I click the Next/Prev buttons - nothing happens at all.
I tried putting a breakpoint in the IntializeDataSource event handler. On the ASPX page where I add the webgrid via the designer, the event fires off every time I click the NEXT or PREV buttons on the pager. But on the other ASPX page, where I added the grid through code-behind, the event handler is not firing.
Anyone got any ideas?
Here's all the code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InfragisticsGrid.aspx.cs" Inherits="MyTestWebApplication.InfragisticsGrid" %><%@ Register Assembly="Infragistics2.WebUI.UltraWebGrid.v8.1, Version=8.1.20081.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"Namespace="Infragistics.WebUI.UltraWebGrid" TagPrefix="igtbl" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>Untitled Page</title></head><body><form id="form1" runat="server"><!-- before grid --><div id="GridPlaceholder" runat="server"> </div><!-- after grid --></form></body></html>
<%@ Register Assembly="Infragistics2.WebUI.UltraWebGrid.v8.1, Version=8.1.20081.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
Namespace="Infragistics.WebUI.UltraWebGrid" TagPrefix="igtbl" %>
<head runat="server">
<body>
<form id="form1" runat="server">
<!-- before grid -->
<div id="GridPlaceholder" runat="server">
</div>
<!-- after grid -->
</html>
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using MyTestData.DataSetTableAdapters;using MyTestData.Properties;using Infragistics.WebUI.UltraWebGrid; namespace MyTestWebApplication{public partial class InfragisticsGrid : System.Web.UI.Page{private UltraWebGrid _ultraWebGrid1;public UltraWebGrid UltraWebGrid1{get {if (_ultraWebGrid1 == null){_ultraWebGrid1 = new UltraWebGrid();}return _ultraWebGrid1; }} protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){FormatGrid();InitDataSource();this.FindControl("GridPlaceholder").Controls.Add(UltraWebGrid1);}}private void InitDataSource(){CommitteeTableAdapter ta = new CommitteeTableAdapter();MyTestData.DataSet.CommitteeDataTable tbCommittee = ta.GetData();DataView dvCommittee = new DataView(tbCommittee);dvCommittee.Sort = "ShortName";UltraWebGrid1.DataSource = dvCommittee;UltraWebGrid1.DataBind();} protected void UltraWebGrid1_InitializeDataSource(object sender, UltraGridEventArgs e){InitDataSource();} private void FormatGrid(){// General properties - set up AJAX pagingUltraWebGrid1.ID = "UltraWebGrid";UltraWebGrid1.Browser = BrowserLevel.Xml; UltraWebGrid1.EnableTheming = false;UltraWebGrid1.Width = Unit.Percentage(100.0);UltraWebGrid1.InitializeDataSource += new InitializeDataSourceEventHandler(UltraWebGrid1_InitializeDataSource);// Display-related propertiesUltraGridLayout layout = UltraWebGrid1.DisplayLayout;layout.Name = "GridLayout";layout.AllowSortingDefault = AllowSorting.OnClient;layout.AllowColSizingDefault = AllowSizing.Free;layout.TableLayout = TableLayout.Fixed;layout.RowSelectorsDefault = RowSelectors.No;layout.AllowColumnMovingDefault = AllowColumnMoving.None;layout.HeaderClickActionDefault = HeaderClickAction.SortSingle;layout.StationaryMargins = StationaryMargins.Header;layout.BorderCollapseDefault = BorderCollapse.Separate;layout.AutoGenerateColumns = false;layout.LoadOnDemand = LoadOnDemand.Xml;layout.Section508Compliant = true;layout.FrameStyle.Width = Unit.Percentage(100.0); // Add one columnUltraWebGrid1.Bands.Add(new UltraGridBand());UltraGridColumn column = new UltraGridColumn(); //"ShortName", "Short Name", ColumnType.NotSet, ""); //();column.BaseColumnName = "ShortName";column.Key = "ShortName";column.Width = Unit.Percentage(100.0);column.ValueList.DisplayMember = "ShortName";column.Header.Caption = "Short Name";UltraWebGrid1.Columns.Add(column);// Paging layout.Pager.MinimumPagesForDisplay = 1;layout.Pager.PageSize = 5;layout.Pager.AllowPaging = true;layout.Pager.ChangeLinksColor = false;layout.Pager.NextText = "Next";layout.Pager.PrevText = "Previous";layout.Pager.StyleMode = PagerStyleMode.PrevNext;// Not sure if I need this...layout.ActivationObject.BorderColor = System.Drawing.Color.Empty;layout.ActivationObject.BorderWidth = Unit.Empty;} }}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyTestData.DataSetTableAdapters;
using MyTestData.Properties;
{
public partial class InfragisticsGrid : System.Web.UI.Page
get {
}
FormatGrid();
InitDataSource();
MyTestData.DataSet.CommitteeDataTable tbCommittee = ta.GetData();
UltraWebGrid1.DataSource = dvCommittee;
UltraWebGrid1.DataBind();
// General properties - set up AJAX paging
UltraWebGrid1.Browser = BrowserLevel.Xml;
UltraWebGrid1.Width = Unit.Percentage(100.0);
// Display-related properties
layout.Name = "GridLayout";
layout.AllowColSizingDefault = AllowSizing.Free;
layout.RowSelectorsDefault = RowSelectors.No;
layout.HeaderClickActionDefault = HeaderClickAction.SortSingle;
layout.BorderCollapseDefault = BorderCollapse.Separate;
layout.LoadOnDemand = LoadOnDemand.Xml;
// Add one column
UltraGridColumn column = new UltraGridColumn(); //"ShortName", "Short Name", ColumnType.NotSet, ""); //();
column.Key = "ShortName";
column.ValueList.DisplayMember = "ShortName";
UltraWebGrid1.Columns.Add(column);
// Paging
layout.Pager.MinimumPagesForDisplay = 1;
layout.Pager.PageSize = 5;
layout.Pager.ChangeLinksColor = false;
layout.Pager.PrevText = "Previous";
// Not sure if I need this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InfragristicsGrid2.aspx.cs" Inherits="MyTestWebApplication.InfragristicsGrid2" %><%@ Register Assembly="Infragistics2.WebUI.UltraWebGrid.v8.1, Version=8.1.20081.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"Namespace="Infragistics.WebUI.UltraWebGrid" TagPrefix="igtbl" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>Untitled Page</title></head><body><form id="form1" runat="server"><div><igtbl:UltraWebGrid ID="UltraWebGrid1" runat="server" Width="905px" Height="238px" Browser="Xml" OnInitializeDataSource="UltraWebGrid1_InitializeDataSource"><Bands><igtbl:UltraGridBand><AddNewRow View="NotSet" Visible="NotSet"></AddNewRow></igtbl:UltraGridBand></Bands><DisplayLayout AllowColSizingDefault="Free"AllowSortingDefault="OnClient" BorderCollapseDefault="Separate"HeaderClickActionDefault="SortSingle" Name="UltraWebGrid1" RowHeightDefault="20px"RowSelectorsDefault="No" TableLayout="Fixed" Version="4.00" UseFixedHeaders="True" LoadOnDemand="Xml"><Pager MinimumPagesForDisplay="2" AllowPaging="True" StyleMode="PrevNext" PageSize="10"></Pager><FrameStyle Height="238px" Width="905px"></FrameStyle></DisplayLayout></igtbl:UltraWebGrid></div></form></body></html>
<div>
<igtbl:UltraWebGrid ID="UltraWebGrid1" runat="server" Width="905px" Height="238px" Browser="Xml" OnInitializeDataSource="UltraWebGrid1_InitializeDataSource">
<Bands>
<igtbl:UltraGridBand>
<AddNewRow View="NotSet" Visible="NotSet">
</AddNewRow>
</igtbl:UltraGridBand>
</Bands>
<DisplayLayout AllowColSizingDefault="Free"
AllowSortingDefault="OnClient" BorderCollapseDefault="Separate"
HeaderClickActionDefault="SortSingle" Name="UltraWebGrid1" RowHeightDefault="20px"
RowSelectorsDefault="No" TableLayout="Fixed" Version="4.00" UseFixedHeaders="True" LoadOnDemand="Xml">
<Pager MinimumPagesForDisplay="2" AllowPaging="True" StyleMode="PrevNext" PageSize="10">
</Pager>
<FrameStyle Height="238px" Width="905px">
</FrameStyle>
</DisplayLayout>
</igtbl:UltraWebGrid>
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using MyTestData.DataSetTableAdapters;using MyTestData.Properties;namespace MyTestWebApplication{public partial class InfragristicsGrid2 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){InitDataSource();}private void InitDataSource(){CommitteeTableAdapter ta = new CommitteeTableAdapter();MyTestData.DataSet.CommitteeDataTable tbCommittee = ta.GetData();DataView dvCommittee = new DataView(tbCommittee);dvCommittee.Sort = "ShortName";UltraWebGrid1.DataSource = dvCommittee;UltraWebGrid1.DataBind();}protected void UltraWebGrid1_InitializeDataSource(object sender, Infragistics.WebUI.UltraWebGrid.UltraGridEventArgs e){InitDataSource();}}}
public partial class InfragristicsGrid2 : System.Web.UI.Page
Hello Lars,
Thanks for sharing your solution in forums, this will certainly be helpful for othe developers. Page_Init is the first event of the page lifecycle (and controls parsed from the ASPX template are available there) so it seems like a good place. If you for some reason later need to use ohter events to add the dynamically created grid to the controls tree of the page, you can also try using the InitializeDataSource event of the grid (instead of binding in Page_Load).
InitializeDataSource is called automatically when the grid needs to be bound with data, may help you further on.
OK I sorted this out myself. I had to move these lines:
this.FindControl("GridPlaceholder").Controls.Add(UltraWebGrid1);
from Page_Load into a Page_Init method on Infragistics.aspx.cs. Then in the Page_Load method I call the method to bind the grid.