serialhobbyist
serialhobbyist

Reputation: 4815

How can I get a variable into Silverlight 3 control from my ASP.NET code?

I'm playing with Silverlight 3 at the mo. I'm trying to get the current user ID into the Silverlight 3 page. I've done a bit of research and initParams seems to be the right way to go. Trouble is, they seemed to use the asp:Silverlight control and that's gone in SL3. I'm stuck trying to get a variable into the initParams list, assuming this is the right way to go.

I've started with a new Silverlight 3 application called "MyFirstSilverlightApp". I've added a code-behind page to the "MyFirstSilverlightAppTestPage.aspx" to allow me to do any clever bits.

I've managed to hard-code an initParam by adding this to the params of the object defenition:

    <param name="initParams" value="userID=id42" />

In App.xaml.cs, I've added the following to Application_Startup:

    string userID = e.InitParams["userID"];

and I've passed that into my page in a parameter in the constructor and then used that in a control. That all works.

What I can't work out is how to get the value from the variable I created in the code-behind into the param name value definition. Any help would be gratefully received.

Upvotes: 3

Views: 1207

Answers (2)

MLT
MLT

Reputation: 514

Add it to your resources collection.

Try something like.

For Each item As KeyValuePair(Of String, String) In e.InitParams
    Resources.Add(item.Key, item.Value)
Next

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189495

One quick a dirty approach would be to use <% %> in the param:-

<param name="intiParams" value="userID=<%=myUserID%>" />

My prefered solution is to create my own Silverlight Web Control that can render the object tag and its contents in manner taylored to my application.

Upvotes: 2

Related Questions