fatiDev
fatiDev

Reputation: 5992

Bind data for an Object Data source in code behind and remove SelectMethod

I need to prevent objectdatasource from auloading data and managing that in the code behind . when I remove the SelectMethod , the following problem occurs

The Select operation is not supported by ObjectDataSource 'DataSource' unless the SelectMethod is specified.

this is my objectDataSource

<asp:ObjectDataSource ID="DataSource"  runat="server" TypeName="declaration_prod_liste"  
      EnablePaging="true" StartRowIndexParameterName="startrows"
      MaximumRowsParameterName="pagesize"
      SelectCountMethod="GetDataSourceCount">
</asp:ObjectDataSource>

Upvotes: 0

Views: 2668

Answers (1)

Sunil
Sunil

Reputation: 21406

You could set/change the SelectMethod of objectdatasource in an appropriate event in code-behind. For example, in code below this is being done in Page_Load event. But, you need to specify its SelectMethod in html code and then just cancel it in Selecting event as shown in second code-snippet.

I am assuming your objectdata source id is DataSource1.

Also, make sure you specify your SelectMethod in hmtl for objectdatasource.

protected void Page_Load(object sender, EventArgs e)
{
   DataSource1.SelectMethod = "SelectMethod";
   GridView1.DataSourceID = DataSource1.ID ;
   GridView1.DataBind();
}

You could cancel objectdatasource autopouplating by canceling in the Selecting event as in code snippet below. Make sure to subscribe to this event in objectdatasource html.

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    //puty your logic when to cancel and when not to cancel
    e.Cancel = true;
}

Html should look like below.

<asp:ObjectDataSource ID="DataSource1"  runat="server" TypeName="declaration_prod_liste"  
      EnablePaging="true" StartRowIndexParameterName="startrows"
      MaximumRowsParameterName="pagesize"
      SelectCountMethod="GetDataSourceCount"
      SelectMethod="SomeMethod"
      OnSelecting ="ObjectDataSource1_Selecting" >
</asp:ObjectDataSource>

Upvotes: 1

Related Questions