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: , , , ,

Asp.Net: Handle Usercontrol ClickEvent in Page

by Pieter Brinkman 24. March 2009 04:01

In this article we're going to create a usercontrol with a button. Place this usercontrol on a page and handle the clickEvent on that page.

Step 1, the UserControl

Create a usercontrol with a button called  'clickMeButton'.

In the codebehind of your UserControl define a delegate.

public delegate void ButtonClickedEventHandler(object sender, EventArgs e);


Then create a public EvenHandler.

public event ButtonClickedEventHandler ButtonClickedEvent;


Now add a click event to the button and on the click event fire tje ButtonClickedEvent.

protected void clickMeButton_Click(object sender, EventArgs e)
{
   ButtonClickedEvent(sender, e);
}

And the usercontrol is finished, now create a page to use the usercontrol.


Step 2, the Page

Create a new page. Add the usercontrol to the page, in the codebehind (Page_Load) attach your page to the ButtonClickedEvent.

UcWithClickEvent1.ButtonClickedEvent += new UcWithClickEvent.ButtonClickedEventHandler(UcWithClickEvent1_ButtonClickedEvent);


Press tab twice to generate the following Method.

void UcWithClickEvent1_ButtonClickedEvent(object sender, EventArgs e)
{
   throw new NotImplementedException();
}


Lets implement some basic logic to the event to test if the event is working

void UcWithClickEvent1_ButtonClickedEvent(object sender, EventArgs e)
{
   Response.Write("Button clicked");
}


Step3, the result

Here is the result after the user clicked the button within the usercontrol. As aspect-ed the Button Clicked text is shown.


You can download the example here (blogExamples.rar (17.87 kb))

Hope it helps!

Tags: , , , ,

ASP.Net | Controls

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am a .NET Software Engineer for Achmea IT in De Meern, The Netherlands. My interests are mainly web applications created with ASP.NET, MSSQL and Silverlight.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS

Most comments

club penguin cheats club penguin cheats
4 comments
us United States
Web Design Company Web Design Company
2 comments
Web design Web design
2 comments
gb United Kingdom