Asp.Net: Using a Usercontrol property with ObjectDataSource selectparameter

by Pieter Brinkman 9. April 2009 09:34

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:

[code:c#]

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

 

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

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


Hope it helps.

Tags: , , , ,

Comments

7/13/2009 12:59:57 PM #

Baby Shower Favors

Wow! This was really helpful. I really appreciate the way you demonstrated this topic with graphical snapshots of the code snippets. This really helped me to understand how it worked.

Cheers, Yameer, Baby Shower Favors

Baby Shower Favors United States

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am Solution Architect for Sitecore in The Netherlands. My interests are mainly ASP.NET, MSSQL and Content Management Systems.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

RecentComments

Comment RSS

Most comments