0

I am trying to get 4 values from the session,while i get two of them,other two are missing.i am still learning so please bear with me if my question is too naive.I have this checkbox and two radio buttons in a tree view.

ASP.NET Code

     <div class="control-label col-sm-1">
   <asp:TreeView runat="server" ID="tvAi" Font-Bold="true" ShowLines="true" EnableClientScript="true"
         ShowExpandCollapse="true" ShowCheckBoxes="All" >
       </asp:TreeView>
 <asp:HiddenField ID="hdMobile" runat="server" />
 </div>

Now when i go to the chrome developer tool and check the values of the radio button the session values are binding well and shows like this.

<input type="radio" checked="checked" id="rad_01-02-00000622" name="rdoC1" value="01-02-00000622_Deposit_1_109861">

Those four values separated by '_' are accountNumber_accountType_officeId_customerId respectively.

The Javascript Code which sets session

   $(function() {
        $("[id*=tvAi] input[type=checkbox]").bind("click",
            function() {


                var table = $(this).closest("table");

                if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
                    //Is Parent CheckBox
                    var childDiv = table.next();
                    var isChecked = $(this).is(":checked");
                    if (isChecked) {
                        if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                            bootbox.alert(
                                "Please enter the Cell Number because you have asked for the MobileBankingService.");
                            this.checked = false;
                            $('#CellNumberTextBox').focus();
                            return false;
                        }
                    }
                    $("input[type=radio]", childDiv).each(function() {
                        if (isChecked) {

                            $(this).attr("checked", "checked");
                            return false;
                        } else {
                            $(this).removeAttr("checked");
                        }

                    });

                }
            });

        $("[id*=tvAi] input[type=radio]").bind("click",
            function() {
                //hdMobile
                var parentDIV = $(this).closest("DIV");
                if ($(this).is(":checked")) {

                    if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                        bootbox.alert(
                            "Please enter the Cell Number because you have asked for the MobileBankingService.");
                        this.checked = false;
                        $('#CellNumberTextBox').focus();
                        return false;
                    }

                    $("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked");
                } else {
                    $("input[type=checkbox]", parentDIV.prev()).removeAttr("checked");
                }
            });
        $("#SaveButton").bind("click",
            function(e) {
                $("#hdMobile").val("");
                var tv = document.getElementById("<%= tvAi.ClientID %>");
                var chkArray = tv.getElementsByTagName("input");
                for (i = 0; i <= chkArray.length - 1; i++) {

                    if (i == 0) {
                        $.ajax({
                            type: "POST",
                            url: "AddNewCustomer.aspx/SetSession",
                            data: {},
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function() {

                            }
                        });
                    }

                    if (chkArray[i].type == 'radio') {

                        if (chkArray[i].checked == true) {
                            if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                                bootbox.alert(
                                    "Please enter the Cell Number because you have asked for the MobileBankingService.");
                                $('#CellNumberTextBox').focus();
                                $.hideprogress();
                                return false;
                          

                            if ($("#hdMobile").val() == "" || $("#hdMobile").val() == null) {
                                $("#hdMobile").val(chkArray[i].value);
                            } else {
                                $("#hdMobile").val($("#hdMobile").val() + "," + chkArray[i].value);
                            }
                        }
                    }
                }

            });
    });

My Code behind setting the session

 [WebMethod]
    public static void SetSession()
    {
        System.Web.HttpContext.Current.Session["AccountMoney"] = "";
    }

And binding treeview code

 private void BindTreeViewForMobile()
{
    _customerId = Convert.ToInt64(Session["CustomerId"]);

    if (_customerId > 0)
    {
        DataTable dtAccount = new DataTable();
        dtAccount = BusinessLayer.SMS.SmsSetup.GetActiveAccountListByCustomer(_customerId);

        tvAi.Nodes.Clear();
        int redindex = 1;

        string accNumber = "";
        var customerMobileBanking = BusinessLayer.SMS.MobileBankingSetup.GetMobileBankingCustomer(_customerId);
        foreach (DataRow dr in dtAccount.Rows)
        {
            if (customerMobileBanking != null)
            {

                foreach (Common.SMS.MobileBankingSetup ss in customerMobileBanking)
                {
                    if (ss.AccountNumber == dr["account_number"].ToString())
                    {
                        if (ss.FeeCharges)
                        {
                            accNumber = ss.AccountNumber;

                            break;
                        }
                        else
                        {
                            accNumber = "";

                            break;
                        }
                    }
                }
            }


            TreeNode master = new TreeNode(dr["account_number"].ToString(), dr["account_number"].ToString());
            master.ShowCheckBox = true;
            tvAi.Nodes.Add(master);
            master.SelectAction = TreeNodeSelectAction.None;
            string sk = "";

            if (accNumber != "")
            {
                if (accNumber == dr["account_number"].ToString())
                {
                    master.Checked = true;
                }
            }


            for (int i = 0; i <= 1; i++)
            {

                TreeNode child = new TreeNode(sk, sk);
                child.SelectAction = TreeNodeSelectAction.None;
                child.ShowCheckBox = false;

                if (accNumber != "")
                {
                    if (accNumber == dr["account_number"].ToString())
                    {
                        child.Text = "<input type='radio' checked='checked' id='rad_" + dr["account_number"].ToString() + "' name='rdoC" + redindex.ToString() + "' value ='" + sk  + dr["account_number"].ToString() + "_" + dr["account"].ToString() + "_" + dr["office_id"].ToString() + "_" + _customerId.ToString() + "' />" + child.Text;
                    }

                }
                else
                {


                        child.Text = "<input type='radio' id='rad_" + dr["account_number"].ToString() + "' name='rdoC" + redindex.ToString() + "' value ='" + sk  + dr["account_number"].ToString() + "_" + dr["account"].ToString() + "_" + dr["office_id"].ToString() + "_" + _customerId.ToString() + "' />" + child.Text;


                }



                master.ChildNodes.Add(child);
            }
            redindex++;
        }
    }

}

The problem happens here when i am trying to get the session value in here

  var sess = System.Web.HttpContext.Current.Session["AccountMoney"].ToString();

Here i only get the two session value not all 4.What am i doing wrong?Code must be too long for your time.any help appreciated.Thanks

OLDMONK
  • 382
  • 2
  • 7
  • 25
  • Either i don't understand your code or your code is irrelevant to the problem you describe. Your SetSession function does nothing, your javascript doesn't send any data. Depending how exactly your setSession looks like the fact that it's a static function may be the source of the problem but without any code in there it's hard to tell. – user5014677 Aug 28 '17 at 08:35
  • @user5014677 sorry for the irrelevant code.i get two of the session values correctly.just stuck with the other two sir.addsession is done in savebutton on click. – OLDMONK Aug 28 '17 at 08:39
  • Complete the SetSession function. In what you have posted you set the variable AccountMoney to an empty string. And in your javascript the data is {} . Your SetSession is a static method. So depending how you actually set the session it can be the issue of your problem since a static function keeps the values in between the calls. But without any actual code in that function it's hard to tell what you're actually doing there. – user5014677 Aug 28 '17 at 08:54

1 Answers1

0

I faced this before and search got me nowhere, went ahead and used cookies for the values I wanna read from JS. it works like a charm! Try it Get Cookies by name JS

  • Be careful with this approach. Unlike session, cookies can be seen and manipulated by the user. There is also a limit to their size *and* they add overhead to every request. – Richard Aug 28 '17 at 09:50
  • I know I know, isn't getting the session value from JS have the same vulnerability! that's what he asked for.. exposing his session value to the clientSide –  Aug 28 '17 at 09:59