Mats Lycken development

.NET and web related blog posts


jQuery part 2 - Event handling

jQuery makes event handling in javascript very easy and extremely handy to perform all kinds of different things in the UI.

The great thing about the event handling is that you can bind to events to almost anything, this makes things like creating hover effects over tables and creating clickable divs a breeze. And since you use the jQuery css style selectors to specify which elements you want to bind the event to, you can bind the event to many elements with one line of code!

I’ll start out by showing you how to bind the click event.


Harness the power of javascript with jQuery

What is jQuery, and why should I use it

Quite many .NET developers avoid using client side scripting and often use post backs to do simple updates that could just as well be done on the client. Using a post back where client scripting would have worked just as well is a real bad thing. It makes the web app experience much slower for the end user since the post back needs to send data to the server, it also bogs down the server with unnecessary requests.

Historically javascript has been quite painful to use due to browser differences that forced us developers to use ugly browser sniffing and create duplicate code targeting different vendors.

But things are different today!

jQuery is a small javascript library that makes creating cross browser javascript a breeze. It is an open source frame work that due to it’s simpleness and extensibility has drawn a great following.


ASP.NET 101 - Part 2: Sending event data in your custom events

One of the key benefits of encapsulating logic and behavior in a user control is that the user of the control doesn’t have to care about the inner workings of the control. We only need to expose the things that the users of the control needs.

We will demonstrate this by creating a simple user registration control. The control will expose an event, UserCreated, which will contain the newly created user as data.

This way the page that the user control is located in doesn’t need to care about what fields the user needs to fill out or how the person is created. It is simply handed a user that was created.


ASP.NET 101 - Part 1: Communication from User control to Page in ASP.NET

User controls are great for encapsulating both behavior and look. When you have some UI elements that you want to use in more than one place a User control is a great way to add reusability.

One problem that you can run into is how you add communication from the user control to the web page. Perhaps you have a button in a user control, and when you click it the web page should perform some action. If the button was on the page we would simply bind to the Clicked event on the button, but when it’s inside of the user control we don’t have access to the button.

Luckily it is really easy in .NET to add your own events. In this really simple example we will create a user control with a button that also has an event, ButtonClicked. We will then create a page that contains an instance of the user control and bind to the exposed event so that the page will know when the button has been clicked.


Adding communication between user controls without involving the web form

One problem I've faced with the model-view-presenter pattern is how to add communication between different user controls on a web form without adding a bunch of logic to the web form. Ideally the web form shouldn't be aware that the user control is there, the user control should be able to load it's data and update the model by itself.

My solution to this problem was to create a custom object builder attribute - CreateShared.

The CreateShared attribute functions the same way as CreateNew except that it will only create one instance / request for the requested type. This can for instance be used in presenters to create a shared instance of the controller.

By doing this you can add events to the controller which reflects changes in the model. The different presenters can listen to these events and respond to changes made by another presenter. This is how you can perform the communication.

You can read a How-To and a description of the sample on the Wiki page on Read it here.

Attached to this entry is both the code and a sample that shows how to use it. Both projects are Visual studio 2008 projects and references Web Client Software Factory 2.0 feb 2008. (31,89 kb)
CreateShared - (742,89 kb)