Using the ViewState within the SelectMethod of a ObjectDataSource

By default it is not possible to use the ViewState within methods of a ObjectDataSource. Because the DataSource doesn’t run within the current page instance, it just fires the method that you specified.

You can set the page instance for the DataSourceby setting the ObjectInstance property in the ObjectCreating event of the DataSource.


<asp:ObjectDataSource ID="odsListing" runat="server" SelectMethod="getItems" TypeName="YOUR.NAMESPACE" OnObjectCreating="ObjectDataSource_ObjectCreating" DataObjectTypeName="List">



protected void ObjectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
 e.ObjectInstance = this;
}


Now you can use the ViewState in you DataSourceMethod (getItems() in this example).

2 Replies to “Using the ViewState within the SelectMethod of a ObjectDataSource”

Leave a Reply

Your email address will not be published. Required fields are marked *