无法在C#中使用javascript获取会话值

时间:2017-08-28 08:12:52

标签: javascript c# jquery asp.net session

我试图从会话中获得4个值,而我得到其中两个,其他两个丢失。我还在学习所以如果我的问题太天真,请耐心等待我。我有这个复选框和两个单选按钮在树视图中。

ASP.NET代码



     <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>
&#13;
&#13;
&#13;

现在,当我转到chrome开发人员工具并检查单选按钮的值时,会话值绑定得很好并且显示如下。

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

由&#39; _&#39;分隔的四个值分别是accountNumber_accountType_officeId_customerId。

设置会话的Javascript代码

&#13;
&#13;
   $(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);
                            }
                        }
                    }
                }

            });
    });
&#13;
&#13;
&#13;

设置会话的我的代码

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

绑定树视图代码

 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++;
        }
    }

}

当我尝试在此处获取会话值时,问题就出现在这里

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

在这里我只得到两个会话值而不是全部4.我做错了什么?代码必须太长,你的时间。非常感谢。谢谢

1 个答案:

答案 0 :(得分:0)

我之前遇到过这种情况,搜索让我无处可去,继续使用Cookie来获取我想从JS读取的值。它就像一个魅力!试一试Get Cookies by name JS