Skip to content

Instantly share code, notes, and snippets.

@malbert69
Last active January 7, 2016 16:07
Show Gist options
  • Save malbert69/f687055b9990923905d2 to your computer and use it in GitHub Desktop.
Save malbert69/f687055b9990923905d2 to your computer and use it in GitHub Desktop.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="do-after-load.ascx.cs" Inherits="controls.DoAfterLoad" %>
<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:HiddenField runat="server" ID="DoAfterLoadJavaScriptHiddenField" />
</ContentTemplate>
</asp:UpdatePanel>
using System;
using System.Collections.Generic;
using System.Linq;
namespace MpFloors.controls
{
public partial class DoAfterLoad : System.Web.UI.UserControl
{
private Dictionary<object, string> _scripts = new Dictionary<object, string>();
//public string DoAfterLoadJavaScript { get; set; }
public void AddDoAfterLoadJavaScript(object key, string script)
{
_scripts[key] = script;
}
public void Clear()
{
_scripts.Clear();
}
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(
GetType(),
"DoAfterLoadJavaScriptEngine",
@"
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function(s, e) {
$(""[id$='_DoAfterLoadJavaScriptHiddenField']"").each(function() {eval(this.value)});
});
", true);
}
protected void Page_PreRender(object sender, EventArgs e)
{
DoAfterLoadJavaScriptHiddenField.Value = string.Join(";",
_scripts.Where(item => !string.IsNullOrWhiteSpace(item.Value)).Select(item => item.Value.TrimEnd(';')));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment