RadControls version
|
2009.1.312.1020 and later. |
.NET version |
3.5 |
Visual Studio version |
VS 2008 SP1 |
programming language |
C# |
browser support |
all browsers supported by RadControls
|
PROJECT DESCRIPTION
Probably most of you do not have the luxury to start over from scratch and
implement pure Silverlight solutions abandoning all of your ASP.NET knowledge
and legacy. We will try to provide a simple demonstration how you can take
advantage of the RadChart for Silverlight and easily add it to your
existing ASP.NET web applications. You'll also see how you can update some of
the chart properties directly from the ASP.NET code.
Let's start with the Silverlight code -- for the sample application I will
demonstrate databinding the chart control to ADO.NET data service that exposes
the "Category Sales for 1997" view from the Northwind database (we will not
go into the details of the auto-generation of the data model and the actual
service that exposes it as this is pretty standard stuff and you can review it in the attached zip file).
Instead we will focus on the Silverlight code that enables RadChart to
consume the data service:
XAML
C#
VB.NET
Now let's move to the ASP.NET part – we have set up a simple ASP.NET website
with a singe page and a user control. As we need to wrap the Silverlight content
inside the user control, first we need to add the following register directive
in the ASCX file:
ASCX
This enables us to reference Silverlight content within the respective ASP.NET
user control via the asp:Silverlight server control (the Source property for
this control is set to the xap output produced by the Silverlight project
discussed above):
ASCX
Basically this is everything that is necessary to display the Silverlight
content inside ASP.NET user control.
However, let's spice it up a bit and add
the option to customize certain properties of the Silverlight chart control
directly from ASP.NET code. First let's sort out what should be done in order to achieve the desired
functionality -- ASP.NET is primarily server-side oriented technology, while the
Siverlight operating ground is the client-side (the user browser). We need a
mechanism that would transfer the server-side property values (from the user
control) to the managed Silverlight code on the client. Fortunately the choice
is easy: JavaScript! Silverlight allows you to create a bridge between the
scripting and managed code worlds and calling managed code from JavaScript is
fairly straightforward process.
There are two steps necessary in order to achieve the desired
functionality:
- You need to decide which member items will be exposed for scriptable access
-- you can do that by applying the
System.Windows.Browser.ScriptableMemberAttribute on the
respective members on the object you will register for scriptable access.
- You need to register the managed object for scriptable access by JavaScript
code. You can do that via the HtmlPage.RegisterScriptableObject(...)
method. This method allows you to give a class instance an alias that
can be used from script. Generally the Silverlight content is exposed by the
plug-in via its content property. You can access your scriptable object by first
retrieving the plug-in, appending the alias onto the content property, and then
referencing the ScriptableMember you want
([silverlight-plugin].content.[scriptable-object-alias].[scriptable-member]
= myValue;).
Let's add the option to customize the following properties from ASP.NET
code:
- The chart title.
- The format of the series item labels.
- The format of the vertical axis labels.
As discussed above we need to register the Silverlight page for
scriptable access and mark three scriptable members like this:
C#
VB.NET
Now let's create the ASP.NET wrapper properties. Remember that we need to
transfer the server-side values via the script bridge to the Silverlight managed
runtime so for each user control property wrapper we are transferring the value
to proxy JavaScript variable via
Page.ClientScript.RegisterStartupScript(...) calls like this
(note we are handling the OnPluginLoaded client-side event of
the asp:Silverlight control in order to ensure that when the script values are
passed to the managed Silverlight runtime, the control is fully loaded and
functional):
ASCX
C#
VB.NET
Hope you will find this information useful.