简体   繁体   中英

Getting value of variable from aspx to codebehind in silverlight

I'm trying to get the time of the initial page load in Silverlight. To do this I want to set a variable in my aspx page, but then add it to my Application.Resources in the code behind to be used at a different time. I've seen that you can use HiddenFields to do this, but how can I do it to save the value of a DateTime, and access it on the other side?

If I understand correctly, you are looking to pass in a value from your host web page to the Silverlight Application. You can send the DateTime in as a parameter to the Silverlight application.

Add the param tag to your Silverlight object tag (note the id and the runat="server").

<param name="initparams" id="initParams" runat="server" value=""/>

Then you can assign its value from the code behind.

initParams.Attributes.Add("value", string.Format("PageLoadTime={0}, DateTime.Now.ToLongDateString());

You can then use it in the Silverlight app.xaml.cs Application_Startup method

if (e.InitParams.ContainsKey("PageLoadTime"))
        {
            this.YourAppLevelVariable = Convert.ToDateTime(e.InitParams["PageLoadTime"].ToString());
        }

**note - this is just a base to get you started. I would add proper DateTime parsing and error handling, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM