Sending Objects from .ASPX page to Silverlight

May 28, 2009 at 10:55 pm Leave a comment

Sending Simple User Defined Objects from ASPX page to Silverlight Application.

This article describes how to send a user defined object created in ASP.Net application across Silverlight application. We will walk though creating a Simple User Defined object in ASPX page called Person and another object called People containing the list of Person.

We will use XMLSerializer to serialize the People class and add the xml content to the InitParametes of the <ASP:Silverlight /> object.

We will then extract the xml from InitParameters in the StartUp_Application event of the Silverlight and fill the data back in the People Class defined in Silverlight application as well.

Creating ASP.Net application to create a list of CustomObject

  1. Create a Silverlight application and choose ASP.Net Web Site to host the Silverlight application.
  2. Open the ProjectName.Web project and open the Silverlight test page.
  3. Add a server tag in the .aspx page and add the code to define the Person.
  4. Mark the class Serializable.
  5. Create another class People which contains the list of Person.
  6. Mark this class as Serializable too.
  7. Create an object of People and add the Person to list of Person.

    <Serializable()> _

 Public Class Person

Implements System.ComponentModel.INotifyPropertyChanged

Public Sub New()

End Sub

Private Sub NotifyPropertyChanged(ByVal info As String)

            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(info))

 End Sub

Private _FirstName As String

Public Property FirstName() As String


                Return _FirstName

            End Get

            Set(ByVal value As String)

                _FirstName = value


            End Set

        End Property


        Private _LastName As String

        Public Property LastName() As String


                Return _LastName

            End Get

            Set(ByVal value As String)

                _LastName = value


            End Set

        End Property


        Private _Age As Integer

        Public Property Age() As Integer


                Return _Age

            End Get

            Set(ByVal value As Integer)

                _Age = value


            End Set

        End Property

Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    End Class


    <Serializable()> _

    Public Class People

        Private _Persons As System.Collections.Generic.List(Of Person)

        Public Sub New()

            Persons = New System.Collections.Generic.List(Of Person)

        End Sub

        Public Property Persons() As System.Collections.Generic.List(Of Person)


                Return _Persons

            End Get

            Set(ByVal value As System.Collections.Generic.List(Of Person))

                _Persons = value

            End Set

        End Property

    End Class

Serializing the list of CustomObject and setting it to InitParameters

  1. Serialize it using XMLSerializer.
  2. Using the StringWriter class, write the serialized string to a Serialized variable.
  3. Set the InitParameters of Silverlight object to “People=” + StringWriterObject.ToString

        Dim xmlFrmtr As New System.Xml.Serialization.XmlSerializer(ppl.GetType)

        Dim txtWrt As New System.IO.StringWriter()

        xmlFrmtr.Serialize(txtWrt, ppl)

        Xaml1.InitParameters = “People=” + txtWrt.ToString

Accessing the InitParameters in the Silverlight application.

  1. Open the Silverlight application and open the App.Xaml.Vb file
  2. In the App Class, add a public property called InitParams as Dictionary of string, string.
  3. In the Application_Startup event set the InitParams equal to e.InitParams
  4. In the Page_Loaded event of Siliverlight in “Page.Xaml.Vb”, create an instance of Application class and assign Application.Current to it.
  5. Create a StringReader and read all the content of People, which is a XML document.
  6. Use the XML Reader to read the xml from the String Reader.
  7. Extract the values from XmlReader and parse it into the People/Person class defined in the Silverlight Application.

        Dim app As App = Application.Current

        Dim xrDr As XmlReader = XmlReader.Create(New System.IO.StringReader(app.InitParams(“People”)))

        Dim ppl As New People()

         While xrDr.Read()

            If xrDr.IsStartElement And xrDr.Name = “Person” Then

                Dim prsn As New Person()


                prsn.FirstName = xrDr.ReadElementContentAsString()


                prsn.LastName = xrDr.ReadElementContentAsString()


                prsn.Age = xrDr.ReadElementContentAsInt


            End If

        End While


Entry filed under: Silverlight. Tags: , , , , .

Silverlight2.0 Nested DataGrid using Silverlight-enabled WCF Services and LINQ to SQL Accessing ScriptableType objects of Silverlight from JavaScript

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

Recent Posts

%d bloggers like this: