all things Sitecore related

Using the ViewState within the SelectMethod of a ObjectDataSource

By on June 3, 2009 in ASP.Net, Controls with 1 Comment

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<Item>">

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

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

About the Author

About the Author: Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of He has more than ten years experience with software developing in multiple programming languages and with different Content Management Systems. Before joining Sitecore Pieter was a lead developer for multiple Sitecore and .Net projects, he joined Sitecore in 2011 as an Solution Architect in The Netherlands, after two years as an Solution Architect he joined the Technical Marketing department. In the role as Techinical Marketing Manager he is responsible for the Global MVP program and the Sitecore technical branding strategy. You can follow Pieter on twitter: @pieterbrink123 or Google+ .


If you enjoyed this article, subscribe now to receive more just like it.

There is 1 Brilliant Comment

Trackback URL | Comments RSS Feed

  1. Joost van Schaik says:

    Nice to see that we are running the same kind of blog 😉

Post a Comment

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