You must Sign In to post a response.
  • Category: ASP.NET

    Why not javascript session value getting into code behind into asp.net

    Hi,
    I am trying getting value of session value from javascript. First time it not getting
    but if I try second time it is getting becuase of first time session value is getting null
    and second time value is storing which is i am trying of password. I can use hidden value
    because of software auditing. I tyring getting value from 'Session["KEY"]' which i have
    used in javascript. and i am using master page. so my script in master page

    <asp:ToolkitScriptManager ID="tsmMains" runat="server" EnablePageMethods="true">
    </asp:ToolkitScriptManager>


    <script type="text/javascript">
    function EncryptPassword() {

    var key = '<%= Session["KEY"] %>';

    if (!document.getElementById('<%=tbPassword.ClientID %>').value == '') {

    var password = document.getElementById('<%=tbPassword.ClientID %>').value;
    var encrypted = CryptoJS.TripleDES.encrypt(password, key);

    document.getElementById('<%=tbPassword.ClientID %>').value = encrypted.toString();

    InitializeRequest(password);
    }
    }
    </script>


    cs.code
    =========

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    this.bLogin.Attributes.Add("onClick", "EncryptPassword()");
    }
    }

    [WebMethod]
    public static string SetDownloadPath(string strpath)
    {
    //Page objp;

    //objp = new Page();

    //objp.Session["KEY"] = strpath;

    nMess = strpath;

    return strpath;
    }
  • #765678
    Hi,
    Refer this:
    ASPX Page:
    $("div.panel button.close").on("click", function (e)
    {
    $.ajax({
    type: "POST",
    url: "WebForm1.aspx/WebFormMethodName",
    dataType: "json",
    //data: szValue,
    data: JSON.stringify({ szValue: 'FName', szVAl2: 'LName' }),
    contentType: "application/json; charset=utf-8",
    error: function (error)
    {
    alert("Operation failed!!!");
    }
    });
    });
    .CS PAGE:
    [WebMethod]
    public static string WebFormMethodName(string szValue, string szVAl2)
    {
    HttpContext.Current.Session["abc"] = szValue;
    return HttpContext.Current.Session["abc"].ToString();
    //HttpContext.Current.Session["abc"] = "";
    //HttpContext.Current.Session.Remove("abc");
    //HttpContext.Current.Session.Clear();
    }

  • #765687
    Hi,

    Please refer below code

    Call the code behind function and assign the session values.

    Javascript Function:

    function InitializeRequest(path) {
    // call server side method
    PageMethods.SetDownloadPath(path);
    }
    Code Behind Function:

    [System.Web.Services.WebMethod]
    public static string SetDownloadPath(string strpath)
    {
    Page objp = new Page();
    objp.Session["strDwnPath"] = strpath;
    return strpath;
    }
    Must enable page methods set to true

    <asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>

    Hope it helps!!!

  • #765688
    Hi,

    Please refer below code

    Call the code behind function and assign the session values.

    Javascript Function:
    Hide Copy Code
    function InitializeRequest(path) {
    // call server side method
    PageMethods.SetDownloadPath(path);
    }
    Code Behind Function:
    Hide Copy Code
    [System.Web.Services.WebMethod]
    public static string SetDownloadPath(string strpath)
    {
    Page objp = new Page();
    objp.Session["strDwnPath"] = strpath;
    return strpath;
    }
    Must enable page methods set to true
    Hide Copy Code
    <asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>

    Hope it helps!!!


Sign In to post your comments