all things Sitecore related

Asp.Net: Using a Usercontrol property with ObjectDataSource selectparameter

By on April 9, 2009 in Uncategorized with 0 Comments

I was using a ObjectDataSource to generate a listing including paging. The ObjectDataSource used the GetItems method of my usercontrol. See this previous post. This construction works great. But now I needed to do some custom filtering in the GetItems method based on a property of the usercontrol. At first I tried to use the property directly in the GetItems method, this didn’t work the property the property returned NULL. After a small search on the internet I found the following blog post.

The solution is to dynamically add a Selectparameter that will be passed to the GetItems method. You can dynamically add Selectparameters at the OnSelecting event of the asp:ObjectDataSource,  like this:


<asp:ObjectDataSource ID="odsListing" runat="server" SelectMethod="getItems"
    TypeName="HUWeb.layouts.HU_GLOBAL.NodeListingSmall"
    DataObjectTypeName="Sitecore.Collections.ChildList"
    onselecting="odsListing_Selecting">
</asp:ObjectDataSource>


And in you codebehind:


protected void odsListing_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
 e.InputParameters["ListingDataSourceId"] = <em>YourUsercontrolProperty</em>;
}


Now you can use YourUsercontrolProperty in the Select method of your ObjectDataSource.


public Sitecore.Collections.ItemList getItems(string <strong>myProperty</strong>)
{
  // Your selection logica
  // You can use the string <strong>myProperty</strong> for filtering
}


Hope it helps.

About the Author

About the Author: Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of Gaatverweg.nl. 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+ .

Subscribe

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

Post a Comment

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

Top