I'm looking to include a JSON string by injecting some JavaScript on each page.
In the master page, I have:
public partial class TheMasterPage : System.Web.UI.MasterPage
{
protected void Page_Init(object sender, EventArgs e)
{
if (Session["TheData"] == null)
{Session["TheData"] = GetData(DateTime.Today.Date); }
}
}
This code checks if the session contains the necessary data for JSON serialization.
I want the data in the session to be added to the JavaScript of all pages.
In the aspx page, I have:
<asp:ContentPlaceHolder id="head" runat="server">
<script type="text/javascript">
var TheJsonData =... ;
</script>
</asp:ContentPlaceHolder>
How can I inject the JSON data there? Which one will be executed first - the aspx injection or the Page_Init function?
For serialization, I use this code:
TheDataList = (List<MyModel>)Session["TheData"];
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new MyModel() });
String result = serializer.Serialize(TheDatatList);
I aim to $(document).ready(function () {ShowData(TheJsonData) }); with TheJsonData already loaded when the document ready event fires.
Thank you.