Hello,
I have some trouble to declare and instantiate dynamically an
ObjectDataSource in the codebehind file. The idea is to bind the
objectdatasource to a gridview and have automate sorting and paging.
The objectdatasource SelectMethod returns a dataview.
When my objectdatasource is declared in the codebehind, sorting is not
working and i receive following error when clicking on a datagrid's
column header. I made the same test declaring the ObjectDataSource in
the designer and sorting works perfectly... :
[HttpException (0x80004005): The GridView 'dgPackages' fired event
Sorting which wasn't handled.]
System.Web.UI.WebControls.GridView.OnSorting(GridV iewSortEventArgs
e) +323
System.Web.UI.WebControls.GridView.HandleSort(Stri ng sortExpression,
SortDirection sortDirection) +101
System.Web.UI.WebControls.GridView.HandleSort(Stri ng sortExpression)
+100
System.Web.UI.WebControls.GridView.HandleEvent(Eve ntArgs e, Boolean
causesValidation, String validationGroup) +595
System.Web.UI.WebControls.GridView.RaisePostBackEv ent(String
eventArgument) +213
System.Web.UI.WebControls.GridView.System.Web.UI.I PostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
+242
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+3840
Here is the declaration with the designer way :
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetPackagesByIdRestaurant"
SortParameterName="SortExpression" TypeName="DB">
<SelectParameters>
<asp:Parameter Name="IdRestaurant" Type="Int32" />
<asp:Parameter Name="SortExpression" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
And here the codebind code version causing troubles :
ObjectDataSource ds = new ObjectDataSource();
ds.TypeName = "DB";
ds.SelectMethod = "GetPackagesByIdRestaurant";
ds.SelectParameters.Add(new Parameter("IdRestaurant",
System.TypeCode.Int32, restaurant.IdRestaurant.ToString()));
ds.SelectParameters.Add(new Parameter("SortExpression",
System.TypeCode.String, ""));
ds.SortParameterName = "SortExpression";
dgPackages.DataSource = ds;
dgPackages.DataBind();
Any help is welcome. Of course i could let the ObjectDataSource in the
designer but i prefer to do it within the code if possible...
Regards
Gilles Faessler