views:

183

answers:

4

Hello! This question about jquery plugin fullcalendar. How I can return to selected date after postback?..

For example, i have week agenda as default view. Assume I choose next week (custom week) and create the event in it. After postback I return to default week, but I want get custom week.

How can be resolved this issue?.. Please help! Thanks

+1  A: 

You have to save the week number the users selected. In PHP you could use $_SESSION to save the selected week and when returning from the Event Creation Process the script gets this variable and shows that week.

session_start();
if (isset($_SESSION["week_number"]))
    $week_number = $_SESSION["week_number"];
else 
    $week_number = $this_week;

echo '<script>var week_number = '.$week_number.'; </script>';

// insert script to show the calendar here, use the variable week_number in your
// Javascript

and when the user clicks the "Next Week" button you have to tell that PHP with an AJAX call to a file like this:

<?php
    session_start();
    $_SESSION["week_number"] = $_GET["new_week_number"];
?>

Send the new week number to the file with the get param, i.e. set_week_number.php?new_week_number=123

opatut
Thank you very much for reply! I understood your idea. I use ASP (with MVC pattern). So I'm not sure that I can generate javascript in server side in this case. May be it's possible to get session parameter via $.ajax({async: false,..}), but it's not very pretty solution.
katrin
Sorry, I don't know anything about ASP... Can't help you with that.
opatut
A: 

You could do a session state as stated above (Here is a link for ASP Specific session state). Under code examples on the linked page are examples of how to save a value to the session state and how to read the value from the session state. And yes you can get the values from the session side to the client side :)

Jake1164
Thank you for reply. How can i pass session parameter to fullcalendar? I use MVC, so i can only pass this parameter to model (and to static view), but i cannot figure out how to pass it to js, only via ajax request..
katrin
A: 

I try to do the follows. Server side:

[HttpPost] public ActionResult Create(FormCollection collection){   
   ...  
   Session["curdate"] = asi.appointmentDate.ToString();  
} 
public ActionResult CurrentDate(){  
   return (Session["curdate"] == null) ? null : Content(Session["curdate"].ToString()); 
}

Client side:

var sd = $.ajax(
                    {
                        url: '<%= ResolveUrl("~/Schedule/CurrentDate") %>',
                        async: false,
                        success: function(data, result) {
                            if (!result)
                                alert('Failure to retrieve the date.');
                        }
                    }
                ).responseText;
        var selectedDate = (sd == "") ? new Date() : new Date(sd);
        $('#calendar').fullCalendar('gotoDate', selectedDate);
katrin
A: 

The javascript can get values from a property, so put the value into the session state before postback. On postback pull the value from the session state into the property. From the client then get the value from the property.

Server Side:

public string bar { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        bar = Session["barvalue"].ToString();        
}

Client Side:

        $(document).ready(function() {
            var foo = '<%= bar %>';
            alert(foo);
        });
Jake1164
Thanks for reply! Page_Load is available in Web Forms, but I use MVC, and cannot access it...
katrin
Fair enough, but why not just put them in the action method?
Jake1164
Yes, I can store it to model and return view with this model in action method. Thanx a lot!
katrin