Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access Session variables in code-behind

In script I set Session.

<script>
    $(document).on('click', '.Pagination', function () {
        '<%Session["Pagination"] = "' + $(this).attr('id') + '"; %>';
        alert('<%=Session["Pagination"] %>');
    });
</script>

Alert works.

I can't access When i want to session from code-behind

if (!string.IsNullOrEmpty(Session["Pagination"] as string))
{
    string Val = Session["Pagination"].ToString();
    Session["Pagination"] = null;
}

String Val equal to ' + $(this).attr('id') + '

like image 695
Chiako Avatar asked Jul 10 '26 03:07

Chiako


2 Answers

I used to put the value in the hidden variable and this will be accessible in code-behind, anyway the session object is active in code behind you can set the variable to session successfully.

Advantage: Globally declared session strings are accessible in code-behind and you can use the variables by calling the property files

ASP

<asp:HiddenField id="session_obejct" runat="server" />

Javascript

document.getElementById('session_obejct').value = "Variable you want to set in session";

C#

session["SESSION_NAME"] = session_obejct.Value;

you can use other methods as well, I hope this will meet your requirement

like image 77
Prajwal Bhat Avatar answered Jul 15 '26 06:07

Prajwal Bhat


I used this way and do it if one have better way pleas tell me

Javascript function:

<script>
    $(document).on('click', '.Pagination', function () {
        PageMethods.NumPagination($(this).attr('id'));
    });
</script>

Code behind :

[System.Web.Services.WebMethod]
public static string NumPagination(string Num)
{
    Page Pagination = new Page();
    Pagination.Session["Pagination"] = Num;
    return Num;
}

if (!string.IsNullOrEmpty(Session["Pagination"] as string))
{
    string Select = "1";
    Select = Session["Pagination"].ToString();
    Session["Pagination"] = null;
}

LINK

like image 29
Chiako Avatar answered Jul 15 '26 04:07

Chiako



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!