How to raise event from the client to the server in a Visual WebGui Silverlight control
In this “How to” we are going to learn how to raise an event from our Visual WebGui Silverlight control client side and how to handle it in the server side implementation of this control.
This topic assumes that you have some basic knowledge of Visual WebGui and what is Visual WebGui over Silverlight. Its also recommended that you read the “How to create property binding in a Visual WebGui Silverlight control”.
The Visual WebGui programming model is event based. When a control changes state, such as when the user clicks a button, it raises an event. This events can be created and immediately raised or they can be created and queued.
First will open our “BindableMyLabel” custom control that we’ve created in “How to create property binding in Visual WebGui Silverlight control” in Visual studio.
What we are going to do is to sign up in the constructor to the MouseLeftButtonDown event.
this.MouseLeftButtonDown += new MouseButtonEventHandler(BindableMyLabel_MouseLeftButtonDown);
Now will add the BindableMyLabel_MouseLeftButtonDown method
void BindableMyLabel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
What we do next is add the code to handle this event
void BindableMyLabel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//Create a new Click event.
Event objEvent = BindingManager.CreateEvent(this, "MouseDown");
//Add a property value to use in the server
objEvent.SetProperty("test", "sss");
//Make sure that this event is critical which means that the
//event was registered and should be raised immediately.
//If the event was not registered the event will be queued and will
//be raised with the next critical event.
if (BindingManager.IsCriticalEvent(this, EventTypes.Click))
{
//Raise the event
BindingManager.RaiseEvents();
}
}
The BindingManager.CreateEvent method have a few overloading. One of them allows you to create the event as unique event so if the user repeats an action a few times but the event was not handheld yet the there wont be a list of events of the same type but just one with the last update of the control state.
Next we override the FireEvent method in the server side of the control and raise our event if the type is MouseDown
protected override void FireEvent(IEvent objEvent)
{
base.FireEvent(objEvent);
if (objEvent.Type == "MouseDown")
{
LabelClick(this, EventArgs.Empty);
}
}
Now will add 2 label to the main form of our application
We’ll sign up to the MouseDown event on one of the labels to show that only when we register the event is raised.|
Add a breakpoint to the fire event method to see this works and that we’ve reached the server from the client.
This event will be fired when the mouse is pressed on the background of the control so I’ve changed the control design to expose the background like this. When you press the mouse button make sure you press on the background
<Grid Background="Azure">
<TextBox Height="20" Text="{TemplateBinding Message}"
Foreground="Red"/>
</Grid>
The result will look like this.
When you press on the background of the second label you can see that the event is not fired and we don’t get to the server. But when the next critical event will be fired this event will reach the server as well.
What we’ve seen in this “How to” is that when we register to an event in the Silverlight control we don’t reach the server but run on the client.
If we want to get to the server we need to raise an event using the BindingManager.RaiseEvents() method that will raise an event to the server and will be handled in the server side of the control (the DHTML class of the control).
We’ve also seen the event queuing mechanism of VWG that prevents form unnecessary trips to the server.
-- Eyal Albert @ Eyal.Albert (at) Gizmox.com