2

I've written a function (addCalendarEvents) which takes in an array (events) and parses into a custom calendar. Everything works perfectly fine when its fired through the document.ready function, events are registering and etc.

Javascript

$(document).ready(function () {
    loadCalendar(null);
    addCalendarEvents([{ title: 'All Day Event', start: '2016-01-01' }, { title: 'Long Events', start: '2016-01-07', end: '2016-01-10' }]);     
});

function addCalendarEvents(events) {
    $('#calendar').fullCalendar('addEventSource', events)
}

However, I also need them to be fired through the code behind to add events dynamically. I've tried using ScriptManager's RegisterStartupScript, but it's not working. Is there a proper way for me to do so?

C# Code Behind

protected void Page_Load(object sender, EventArgs e) 
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "addEvents", "addCalendarEvents([{ title: 'Other Event', start: '2016-01-01' }, { title: 'Other Long Events', start: '2016-01-07', end: '2016-01-10' }]);", true);
}

2 Answers 2

0

It might be simpler to use a <asp:Literal id="StartupScript" runat=server /> control containing the $(document).ready() function to inject your page load callouts.

You could write the statements to the literal control within your Page_Load event and they should get executed by the client when the page is ready.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <asp:Literal ID="CodeInject" runat="server"/>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

And the code behind is:

 protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder SB = new StringBuilder();
        SB.Append("<script>");
        SB.Append("$(document).ready(function () {");

        // statements here
        SB.Append("alert('test');");


        SB.Append("});</script>");
        CodeInject.Text = SB.ToString();
    }
Sign up to request clarification or add additional context in comments.

6 Comments

You can't just put an aspx control in the document.ready() function. How do you propose they do this?
It's admittedly been a while since I worked with asp.net controls, but I do remember writing the entire tag including the <script> and the document.ready function to an asp literal. You might need to do something odd like break apart the "<script>" tag to get it to allow it. This old question does something similar: stackoverflow.com/questions/1982099/…
I modified my answer with a quick example. I tested it locally and it does execute the statements on page load. Hope that helps!
@NickT Thanks, your method is working. I tried initializing the calendar above the Literal control, then the adding the events within the Literal control and it didn't work. I tried it again, this time initializing and adding events within the Literal control and it works, weird uh.. I think the calendar plugin only allows events to be parsed in on the on page load function, when the calendar is initialized.
@WaftureRobin I'm glad you got it working! I was going to suggest that you still needed the SB.Append("$(document).ready(function () {"); and ` SB.Append("});</script>");` parts of the example.
|
0

try to convert the data into json while adding the script. Add your assemblies

using this Assembly
using System.Web.Script.Serialization;

Then Deserialize your object

var json = new JavaScriptSerializer().Serialize(obj);

then call registerStartupScript

ScriptManager.RegisterStartupScript(this, this.GetType(), "addEvents", json, true);

and at client side do this to convert it into json

var obj = JSON.parse(string);

4 Comments

so what should 'obj' be, a string? Lets say if i want to parse in '[{ title: 'All Day Event', start: '2016-01-01' }, { title: 'Long Events', start: '2016-01-07', end: '2016-01-10' }]' how can i do so? Do i have to create a class which houses the property 'title', 'start' and 'end'?
obj would be c# object of any class that you want to convert in to json string or create you own class than convert it into json it depends how you want to see your json yes u are right u have to create the class pouplate it with your data than convert it in to json to use on front end
Check out my posted answer. I've tried using your method, but it's not working.
it's ok the only thing that you have to do is var obj = JSON.parse(string); at client side

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.