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.

9
  • 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. Commented Aug 19, 2013 at 18:09
  • Hi @KevinB thanks for the prompt reply, can you tell me is there be any other possible solution for this. Commented Aug 19, 2013 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? Commented Aug 19, 2013 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. Commented Aug 19, 2013 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 Commented Aug 19, 2013 at 18:20

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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"
    }
});

Comments

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

Comments

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.