0

I have this issue calendar project that I am working with. It has a javascript running to add events. I wanted to get those events and save it in MS SQL. However I have no Idea. If i can get the javascript events to C# code behind I can save it. If you can help me get the javascript data to C#.net or if you can help me to directly save javascript to MS SQL that would be helpful. Here is part of the javascript intended to create event on a calendar.

   select: function (start, end, allDay) {
                bootbox.prompt("New Event Title:", function (title) {
                    if (title !== null) {
                        calendar.fullCalendar('renderEvent',
                            {
                                title: title,
                                start: start,
                                end: end,
                                allDay: allDay,
                                className: 'label-info'
                            },
                            true 
                        );
                    }
                });
                calendar.fullCalendar('unselect');
            }
Jon
  • 9,156
  • 9
  • 56
  • 73
Kish Rosa
  • 11
  • 1
  • You need to understand the difference between `Client` side and `Server`. Database usually exists on `Server` side, so you can't save directly to your DB from `JavaScript` code. – SᴇM Oct 02 '17 at 06:22
  • You can't pass data from `C#` to `JavaScript` ,but you can pass data from `Client` to `Server`. – Feras Al Sous Oct 02 '17 at 06:25
  • Thanks on that. can you give me an Idea on how I can call the variables of this js to my code behind so that I can store it DB. Thanks. – Kish Rosa Oct 02 '17 at 06:25
  • Mr. Feras, can you enlighten me on that? Thanks. – Kish Rosa Oct 02 '17 at 06:25
  • I pass the data from C# to this: events: [ <%=CalendarData%> ] I use the public string of C#.net CalendarData which gets data from MS SQL. However, I wanted to store the data from js to MS SQL now. I am having hard time. – Kish Rosa Oct 02 '17 at 06:27
  • 1
    @KishRosa You can show this link https://en.wikipedia.org/wiki/Ajax_(programming) to know how implement that. – Feras Al Sous Oct 02 '17 at 06:28
  • I understand now. Thanks So much. I saw this example. Instead I will call a function in C#.net from JS. https://stackoverflow.com/questions/18441194/how-to-call-a-c-sharp-function-from-javascript – Kish Rosa Oct 02 '17 at 06:31
  • Certainly you can use `jQuery.ajax()` method, ensure that the data submitted in proper format. The URL part should point to the ASPX page which accepts either query string argument or postback argument. – Tetsuya Yamamoto Oct 02 '17 at 06:51

1 Answers1

0

One of the few and best ways is using a hidden field like this:

  • In aspx code:

    <asp:HiddenField ID="HiddenField1" runat="server" />
    
  • In code behind set it's value:

    HiddenField1.Value = Value
    
  • In javascript you can access it like this:

    document.getElementById('HiddenField1').value
    

    OR the other way around if you want to send from Js to C#:

  • in javascript:

    document.getElementById('HiddenField1').value=Value
    
  • In code behind:

    a=HiddenField1.Value
    
JKOU
  • 255
  • 2
  • 14