I would sincerely appreciate it if someone could explain the following issue experienced w/ 2 different methods of GridView creation for an ASP.NET 2.0 page. In the following code snippet taking from one of my .aspx files I create a GridView bound to the ObjectDataSource shown. That ObjectDataSource is bound to a BL layer class and method that simply returns a DataTable object (SelectMethod="getSysDataTable"). Paging and Sorting are set to True and each of those functions works perfectly. In the code-behind file for this .aspx file there is no code or events written for the GridView (no paging or sorting event handlers), just a few button and text box handlers on that page that are needed. The paging and sorting of the presented data works w/o any issue.
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AllowPaging="True" AllowSorting="True" EmptyDataText="No records found"
PageSize="5" >
DataTextFormatString="Select" HeaderText="Edit" />
On another .aspx page however, I am doing a similar GridView w/ an ObjectDataSource only it is done in a slightly different manner
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AllowPaging="True" AllowSorting="True" EmptyDataText="No records found" PageSize="5">
DataTextFormatString="Select" HeaderText="Edit" />
.
Note the main difference (aside from the bound columns) - no ObjectDataSource is specified on the .aspx page hence the DataSourceID property of the GridView is not set yet. However, now in the .aspx.cs code-behind for this page, I do the following:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
ObjectDataSource obds = new ObjectDataSource();
obds.TypeName = "BatchSummaryVwDAO"; // again a simple BL class that calls an external API that returns a typed Array[] of objects.
obds.SelectMethod = "getBatchSummaryDataTable"; // a method on that class that then returns a DataTable object created from the typed Array[] of objects.
obds.DataBind();
GridViewMonitor.DataSource = obds;
GridViewMonitor.DataBind();
}
The page renders fine initially w/ the GridView data displayed - but if I attempt to "page" by clicking the paging control or "sort" by clicking one of the column header titles, I get unhandled Exceptions thrown stating either:
GridViewMonitor fired event PageIndexChanging which wasn't handled
or
GridViewMonitor fired event Sorting which wasn't handled.
The BIG QUESTION - why in the first scenario does paging and sorting work w/o having to write handler logic for each yet in the second scenario, you get these exceptions. It seems that I am wiring everything together and binding to DataTables the same way...in one case "declaratively" in the aspx page and in the other programmatically in the .aspx.cs code-behind. Any advice would be really appreciated.
Thanks in advance