Ryan S
Ryan S

Reputation: 3268

Populate variables through code behind before the ASCX file is loaded

I have this code which is used to show a Flash Clock on my website. I am getting the source and time from the code behind and pass it through to the flash swf as json paramaters within the ASCX file, as below:

<li class="clock">
  <span id="divAnalogClock">
    <script type="application/json">
      {settings:{id:'Clock', src:'<%# AnalogClockUrl %>', height:22, width:22, color:"#ffffff", allowScriptAccess:'sameDomain', align:'middle', installmessage:'', flashvars:{time:"<%# SiteTime %>", color:"#FFFFFF"}}}
    </script>
  </span>
</li>

Which would generate something similar to:

<embed height="22" flashvars="time=<%# SiteTime %>&amp;color=#FFFFFF" 
       pluginspage="http://www.adobe.com/go/getflashplayer"
       src="<%# AnalogClockUrl %>" type="application/x-shockwave-flash"
       width="22" quality="high" wmode="transparent" allowscriptaccess="sameDomain" 
       allowfullscreen="false" scale="exactFit" id="analogClock" color="#ffffff"
       align="middle" name="analogClock">

The variables AnalogClockURL and SiteTime are being generated on Page_Load:

void Page_Load(object Sender, EventArgs e)
{
    LoadClock();
}

private void LoadClock()
{
    var flashList = CmsHelper.GetFlashList(new List<string> { "Clock" });
    var flash = flashList.SingleOrDefault(f => f.Key.Equals("Clock"));

    if (flash != null)
    {
        AnalogClockUrl = flash.FlashUrl;
    }

    SiteTime = string.Format("{0:HH}:{0:mm}:{0:ss}", TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, Techsson.SiteIntegration.Core.Http.RequestContext.Current.Market.TimeZone));
}

However, The ascx is being triggered first, hence the fields are being left empty and when the Page Load is kicked in, it is too late (from the debugging I was doing). What should I do to ensure that the variables are populated first?

I am also open to other suggestions which solve this issue, in case what I am after is not the best way around it.

Upvotes: 0

Views: 107

Answers (1)

DoctorMick
DoctorMick

Reputation: 6793

Change <%# to <%=

<%# is used for data binding but you have nothing being bound. You could, if you really wanted to, add a runat="server" to your embed markup and then add analogClock.DataBind() in your Page_Load event but that seems unnecessary in this instance.

Upvotes: 2

Related Questions