Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

What's the most straightforward way of passing a post p... RSS

15 replies

Last post Jul 07, 2008 03:11 PM by Jonathan Shen – MSFT

(0)
  • peterdungan

    peterdungan

    Member

    232 Points

    178 Posts

    What's the most straightforward way of passing a post parameter in a webpage to a silverlight app...

    Jul 05, 2008 01:22 PM | LINK

    A user can open different diagrams in a silverlight application, which are stored in a db.

    I'm thinking the best way of implementing this is to have a gridview on an aspx page with links that pass the diagram id as a post parameter to the page containing the silverlight application. What is the most straightforward way of accessing the post parameter from the silvelight app?

    Or would it be better to have the user select the diagram they want to access through the silverlight application?

    Thanks

     

  • texmex5

    texmex5

    Contributor

    2239 Points

    382 Posts

    Re: What's the most straightforward way of passing a post parameter in a webpage to a silverlight...

    Jul 05, 2008 01:37 PM | LINK

    To pass parameters to a Silverlight application you would use initParams variable initsializing the application. You can read about it in here (documentation).

    Here is a sample how you would retrieve these parameters. Keep in mind that you can access initParams values only in Application_Startup method (I can't find a reference to this fact but remember it clearly).

    <param name="initParams" value="myVariable1=Hello,myVariable2=12" />

    And you would retrieve the value in C# (method Application_Startup class App.cs):

    e.InitParams["myVariable1"].ToString()

    e.InitParams["myVariable2"].ToString()

  • peterdungan

    peterdungan

    Member

    232 Points

    178 Posts

    Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a silverl...

    Jul 05, 2008 04:19 PM | LINK

    It says in the page you link to: 

    "The following JavaScript example shows how to specify the initParams parameter value in the user-defined CreateSilverlight method in CreateSilverlight.js."

     I'm confused because I don't have a createsilverlight.js file. I'm using beta 2.

  • texmex5

    texmex5

    Contributor

    2239 Points

    382 Posts

    Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a silverl...

    Jul 05, 2008 04:36 PM | LINK

     just ignore this sentence... you can do the same things if you are not using Silverlight.js but object tag.

    <object data="data:application/x-silverlight," type="application/x-silverlight-2-b2">

    <param name="source" value="MySilverlightApp.xap"/>

    <param name="initParams" value="myVariable1=Hello,myVariable2=12" />

    <a href="http://go.microsoft.com/fwlink/?LinkID=108182">

    <img src="http://go.microsoft.com/fwlink/?LinkId=108181"  alt="Get Microsoft Silverlight" />

    </a>

    now in your silverlight application App.cs you capture the values of the variables like I showed before:

    e.InitParams["myVariable1"].ToString() // this gives you Hello

    now you can pass this value to your app for example in the constructor

    this.RootVisual = new Page(e.InitParams["myVariable1"])

    and in the Page constructor do the stuff you need according to the given parameter.

     

  • MontrayDavis

    MontrayDavis

    Member

    238 Points

    70 Posts

    Re: Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a sil...

    Jul 05, 2008 04:53 PM | LINK

     Yeah, this is something I can't figure out either. My parameters are never found, for some reason....(They seem to never exist).

  • peterdungan

    peterdungan

    Member

    232 Points

    178 Posts

    Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a silverl...

    Jul 05, 2008 05:12 PM | LINK

    "<param name="initParams" value="myVariable1=Hello,myVariable2=12" /> "

    How do I get post parameters from the url though?

    for example the url

    diagramtool.aspx?diagramid=123

     would result in the silverlight app on diagramtool.aspx being passed the parameter diagramid=123

    I haven't used javascript much.

     

  • MontrayDavis

    MontrayDavis

    Member

    238 Points

    70 Posts

    Re: Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a sil...

    Jul 05, 2008 05:21 PM | LINK

    If you are using Javascript, then you should post this in the Javascript section, they can help you  with this issue.

  • peterdungan

    peterdungan

    Member

    232 Points

    178 Posts

    Re: Re: Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a...

    Jul 05, 2008 05:27 PM | LINK

    I'm not using javascript. But I think I need to use it to pass the post variables from the url to the silverlight app. I'll post a link to this in the javascript section.

  • texmex5

    texmex5

    Contributor

    2239 Points

    382 Posts

    Re: What's the most straightforward way of passing a post parameter in a webpage to a silverlight...

    Jul 05, 2008 06:27 PM | LINK

    For future reference. Here is a working sample on how you can pass initsialization parameters to a Silverlight application:

    MyWebPage.html

    <body>

    <div id='errorLocation' style="font-size: small;color: Gray;"></div>

    <div id="silverlightControlHost">

    <object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">

    <param name="source" value="SilverlightApplication1.xap"/>

    <param name="onerror" value="onSilverlightError" />

    <param name="background" value="white" />

    <param name="initParams" value="variable=hello" />

    <a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">

    <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>

    </a>

    </object>

    <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>

    </div>

    </body>

    Page.xaml

    <UserControl

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    x:Class="SilverlightApplication1.Page"

    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White" >

    <Button x:Name="button" Height="100" Width="100" Content="Button"/>

    </Grid>

    </UserControl>

    Page.xaml.cs

    public Page(String ini)

    {

    // Required to initialize variables

     

    InitializeComponent();

    button.Content = ini;

    }

    App.cs (method Application_Startup)

    private void OnStartup(object sender, StartupEventArgs e)

    {

    // Load the main control here

    var a = e.InitParams.Count; // just to debug. Gives the number of initialization parameters provided var b = e.InitParams["variable"].ToString(); // gives the value of init parameter thats name is variable. this case hello

    this.RootVisual = new Page(a.ToString() + " " + (String)b); 

     // this creates a new instance of the Page class. It has a button which  content will be "1 hello"

    }

     

  • texmex5

    texmex5

    Contributor

    2239 Points

    382 Posts

    Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a silverl...

    Jul 05, 2008 06:31 PM | LINK

    oh and i found a javascript that might be useful for geting GET values.

    http://www.11tmr.com/11tmr.nsf/D6Plinks/MWHE-695L9Z