0

I have a shopping cart, for the Time Slot selection for product delivery i want to display Date for the next one week, First Column(Per Day in Per Row), and TimeSlots in another column, the format like below:

Column1                            Column2
Aug 19, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 20, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 21, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 22, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 23, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 24, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM
Aug 25, 2013    [RadioButton]10:00AM    [RadioButton]12:00PM

I tried using gridview with radiobuttonlist (for Time Slots), but the problem is user is able to select Time for Every Days. So as an alternative i am trying to check the TimeSlot selected at the javascript end and because i want the Slot accessible for next checkout wizard, i want to store the selected time slot in the session. I am able to validate the Time Slot if not selected, and get the value in the alert, if selected. However when i try to save the value to session from hidden field (saved on button click at client end), i am not getting the session value to other pages. Below is the code i am trying:

 <script>
        function SaveToHiddenField() {
            if ($("input:radio:checked").length > 0) {
                $radio = $("input:radio:checked");
                document.getElementById('<%= hdnField.ClientID %>').value = $radio.val();
                <% Session["Slot"] = hdnField.Value; %>
                window.location.href='default2.aspx'
                return true;
            }
            else {
                alert("Nothing Selected");
                return false;
            }
        }
    </script>

<asp:Button ID="btnSession" runat="server" Text="Save to Session" OnClientClick="return SaveToHiddenField()" />
<asp:HiddenField ID="hdnField" runat="server" />

Can anyone tell me whats wrong i am doing, or any other alternative which could be the better option for my query.

Abbas
  • 4,948
  • 31
  • 95
  • 161
  • client-side javascript doesn't have direct access to session variables because client-side javascript runs on the client, and session variables exist on the server. – Kevin B Aug 19 '13 at 18:09
  • Hi @KevinB thanks for the prompt reply, can you tell me is there be any other possible solution for this. – Abbas Aug 19 '13 at 18:10
  • @Abbas Could use ajax, though the value is being saved to a hidden field. Why can't you read that and update the session during the postback from clicking the save button? – Jason P Aug 19 '13 at 18:11
  • Yes, use cookies, or send an ajax request to the server with the data that you wish to place into the session, or pass that data as a GET param when you do window.location. – Kevin B Aug 19 '13 at 18:12
  • Hi @JasonP, do you mean i need a postback for saving the hidden field value to the session variable, can't i store it at the same time, as i am doing in the above example, you can check, on the very next line of saving the value to the hidden field, i am storing it in Session variable also, but thats not working – Abbas Aug 19 '13 at 18:20
  • @Abbas No, you can't. All server-side snippets are executed on the server as the page is being rendered. No server-side code makes it to the client. You can see this by viewing the source of the page. The line that saves the hidden field value to the session won't even be there. – Jason P Aug 19 '13 at 18:23
  • Hi @JasonP, if you don't mind can you please upload some script, for the solution you are talking about. – Abbas Aug 19 '13 at 18:36
  • You can refer to this link : [Get/Set Session Object value in JavaScript](http://stackoverflow.com/questions/15519454/how-can-i-access-session-variables-and-set-them-in-javascript) – Manish Mistry Oct 21 '16 at 07:37
  • You can get reference fron following Link : http://stackoverflow.com/questions/15519454/how-can-i-access-session-variables-and-set-them-in-javascript – Manish Mistry Oct 21 '16 at 07:42

3 Answers3

1

Issue is resolved, on the button click i wrote below code:

Session["Slot"] = hdnField.Value;
        Response.Redirect("default2.aspx");

Now when the page is redirect to default2.aspx, i have the session value.

Thanks Everyone,
Specially @JasonP.

Abbas
  • 4,948
  • 31
  • 95
  • 161
0

You can use ASP.NET AJAX Page Methods to store and retrieve session values, like this:

Code-behind for default1.aspx:

[WebMethod(EnableSession = true)]
public static void StoreSessionValue(string theValue)
{
    HttpContext.Current.Session["TheValueToStore"] = theValue;
}

Now in your markup for default1.aspx, use jQuery to call this page method, like this:

$.ajax({
    type: "POST",
    url: "defaul1.aspx/StoreSessionValue",
    data: "{'theValue': document.getElementById('<%= hdnField.ClientID %>').value}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        window.location.href='default2.aspx';
    }
});

Code-behind for default2.aspx:

[WebMethod(EnableSession = true)]
public static string RetrieveSessionValue()
{
    return HttpContext.Current.Session["TheValueToStore"].ToString();
}

Now in your markup for default2.aspx, use jQuery to call this page method, like this:

$.ajax({
    type: "POST",
    url: "defaul1.aspx/RetrieveSessionValue",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        // Do something with the session value that came back in the variable "msg"
    }
});
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

In C#

Session["MySes"] = "SessionValue";

In Javascript

 var ScriptSes = "<%= Session["MySes"]%>"; 

in html

 <input type="button"     value="Save to Session"  onclick="SaveToHiddenField()" />

In html use html button tag when you use asp.net button control it loads page then your variable session value destroy so you cannot get session value . so use html input type button instead of asp.net button controller

User
  • 1,334
  • 5
  • 29
  • 61