DCSIMG
January 2009 - Posts - Itai Goldstein

January 2009 - Posts

Transform List<T> to List<K> where T inherits from K

Download ListExtension.cs file :

Sometimes it is useful to copy one list contents to another which its type is more general.

Let me emphasize my intent with a small example:

Let there be a Dog, Cat & a Mouse types which inherit from Animal type, and let there be some lists: List<Dog>, List<Cat> & List<Mouse> list that we would like to union to one List<Animal> list in order to perform an general operation on all of the animals objects.

Here’s an example of these classes implementation for clarifying purpose:

image

We can try adding the specific lists to the general list (e.g.: adding List<Dog> to List<Animal>):

image

Casting dogs list to List<Animal> is also not allowed.

So we need a way to transform the specific type list to its general type form, something like the following:

image

In order to enable us to perform the precedes we’ll use the Extension Method feature in .NET 3.5 to extend the List<T> type with the Transform method:

image

Note how the Transform method signature enforces T type to inherit from K type using the where keyword.

Summary

In this post I reviewed a way to transform one typed generic list List<T> to another typed generic list List<K> in case that the type T inherits from the type K and used Extension Method feature to add this capability to the List<T> class.

Hope you’ll find it useful.

kick it on DotNetKicks.com

Share it:

Exposing User Control client events to host Page server side

Download Source Solution:

Server side events handling is a great feature of ASP.NET. You can enjoy your server side framework which is usually easier to maintain than your client side code.

One of the drawbacks of server side events handling is the Postback, in which the request is send to the server with all its weight and the response renders the whole HTML again - the user suffers from a visual effect in which the page he’s viewing disappears and reappearing after it finished his rendering process again.

Of course with ASP.NET AJAX, we now have the great UpdatePanel control which hides the postback visual effect from our users and reduces the request weight and renders the HTML part we intended to update. Still, the UpdatePanel control has some drawbacks which one of them is that it carries extra data with it.

Consider a scenario in which you have your user control hosted inside a page and you would like to handle one of your user control’s events on your hosting page, but you want to do it on the server side of your page, and you don’t want to postback the page for this task.

In order to achieve the effect that is described above, we can use a ASP.NET 2.0 feature which is called Callback, which will start the bubbling process we would like to achieve.

Our user control will have to include an event handler in order to bubble the event to his hosts (the hosting page in our case):

image

image

Our user control would implement the ICallbackEventHandler in order to use the Callback feature:

image

The interface implementation involves two methods:

image

What is left for us to do is setting the code for the client side. We’ll start with preparing the code for the Callback at our user control PreRender event handler:

image

The code which is written above does the following: it creates a JavaScript function called BubbleEvent which would contain the Callback call to the server side (the actual call is generated by the ClientScriptManager.GetCallbackEventReference method) and the reference to another JavaScript function – ReceiveServerEventBubbleResponse – which will be called when the server side work ends. The JavaScript script is injected to the control by the ClientScriptManager.RegisterClientScriptBlock method.

Next, we need to define our control HTML and JavaScript as the following:

image

And now all there is left for us to do, is using our user control in a page. First register the control:

image

Then place it into the page HTML:

image

And now just register to the event and define the event handler on the page server side code:

image

Summary

In this post I reviewed a way to bubble client event to the server side, so I can use the power of the server side without the drawbacks of the postback.

Hope you’ll find it useful.

Share it:
Posted by itai | 3 comment(s)

Good Morning, World!!!

Not sure whether it is morning when you read this post, but I always thought Robin Williams screaming at Good Morning Vietnam was a good way to start the day.

My name is Itai Goldstein, and I’m a consultant and software developer in the BPM (Business Process Management) group within SRL group. Currently I’m working on BPM projects as AgilePoint is the BPM engine we use to implement the solutions with our customers. Previously I mainly developed ASP.NET applications for the enterprise and world wide use.

I would like to invite you to check this blog, in which I’m gonna write about .NET, programming and my real world adventures in the development field..

Stay tuned!

Posted by itai | 9 comment(s)
תגים: