开发者

Add InitParam to silverlight object, from Code/Javascript/jQuery

开发者 https://www.devze.com 2023-04-03 07:47 出处:网络
Is there a way, to change initParam parameter, on my silverlight object, from my code? Can\'t jQuery d开发者_如何转开发o this, somehow?

Is there a way, to change initParam parameter, on my silverlight object, from my code? Can't jQuery d开发者_如何转开发o this, somehow?

I have my silverlight object, in a div that is display:none, and before I show it, I would like to add some InitParams.


Yes you can do it. The following provides the code with server controls rather than a JQuery.

You can add the initParam attribute in the object tag of your aspx page where yout silverlight app is hosted like below.

<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="ClientBin/SilverlightApplication3.xap"/>
      <param name="onError" value="onSilverlightError" />
      **<param name="initparams" id="initParams" runat="server" value="key=value"/>**
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="4.0.50826.0"  

in the same aspx page you can have an event where it sets the values for the param like below

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
    initParams.Attributes.Add("value",  TextBox1.Text);
}
</script>

Then you can access the value of the initParams in the App Startup code in the silverlight application like below.

 private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (e.InitParams.ContainsKey("param"))
            MessageBox.Show(e.InitParams["param"]);

Make sure you provide the value for the initParams in the following format and also check in the code for the existance of the key.

key1=value1, key2=value2

Alternatively, you can use the Silverlight.createObject and pass the initParameters as well Refer to the link http://msdn.microsoft.com/en-us/library/cc265155(v=vs.95).aspx

Hope this helps.


The approach I would take in this case is just to leave a place hold <div /> where I want to put the Silverlight object. Include in the page the Silverlight.js file and use the createObject() method to create the plugin along with all its properties and initParams when necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消