【问题标题】:Cant get the session value using javascript in C#无法在 C# 中使用 javascript 获取会话值
【发布时间】:2018-02-05 10:57:56
【问题描述】:

我正在尝试从会话中获取 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>

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

&lt;input type="radio" checked="checked" id="rad_01-02-00000622" name="rdoC1" value="01-02-00000622_Deposit_1_109861"&gt;

用'_'分隔的这四个值分别是accountNumber_accountType_officeId_customerId。

设置会话的Javascript代码

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

            });
    });

设置会话背后的我的代码

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

并绑定treeview代码

 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.我做错了什么?代码必须太长,不适合您的时间。感谢任何帮助。谢谢

【问题讨论】:

  • 要么我不理解您的代码,要么您的代码与您描述的问题无关。您的 SetSession 函数什么都不做,您的 javascript 不发送任何数据。取决于您的 setSession 看起来如何,它是一个静态函数这一事实可能是问题的根源,但如果没有任何代码,就很难说出来。
  • @user5014677 抱歉,代码不相关。我正确获取了两个会话值。只是卡在其他两个sir.addsession 是在点击保存按钮中完成的。
  • 完成SetSession函数。在您发布的内容中,您将变量 AccountMoney 设置为空字符串。在您的 javascript 中,数据是 {} 。您的 SetSession 是一个静态方法。因此,取决于您实际设置会话的方式,这可能是您的问题,因为静态函数将值保留在调用之间。但是如果没有该函数中的任何实际代码,就很难说出你实际上在做什么。

标签: javascript c# jquery asp.net session


【解决方案1】:

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

【讨论】:

  • 谨慎使用这种方法。与会话不同,cookie 可以由用户查看和操作。它们的大小也有限制并且它们会为每个请求增加开销。
  • 我知道我知道,不是从 JS 获取会话值有同样的漏洞!这就是他所要求的......将他的会话值暴露给客户端
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
相关资源
最近更新 更多