从后端检索JavaScript变量的值

3

我的整个javascript代码都在后端(code behind)中。我想要在后端获取javascript变量EmailId的值,用于我的ajax数据。但是因为我必须将它写在双引号中,所以会导致很多问题。以下是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    //HttpResponse response = HttpContext.Current.Response;
    //response.AddHeader("X-Frame-Options", "GOFORIT");

    //Response.AddHeader("X-Frame-Options", "GOFORIT");
    Session["txtEmail"] = txtEmail;

    if (!IsPostBack)
    {
        GetItemsList();
        Session["ProfileImage"] = imgProfilePicture;

        if (Convert.ToBoolean(Session["GotToken"]) == true)
        {
            GetProfilePic();
        }

        if (HttpContext.Current.Request.QueryString["code"] != null)
        {
            Session["code"] = HttpContext.Current.Request.QueryString["code"];
            string success = TestFB.AllowAppAuthentication(Convert.ToString(Session["ItemID"]));
            StringBuilder sb = new StringBuilder();
            var javacriptVariable = "<%= $('#txtEmail').val() %>";
            sb.Append("jQuery(function() { ");
            sb.Append(@"jQuery('#dialog-form').dialog({
                                autoOpen: true,
                                height: 500,
                                width: 500,
                                modal: true,
                                buttons: {
                                    'Participate': function () {
                                        var bValid = true;                         
                                        var EmailId = $('#txtEmail').val();

                                        var chkTermsAndConditions = document.getElementById('chkTermsAndConditions').checked;
                                        if (bValid && chkTermsAndConditions == true) {
                                            console.log(EmailId);
                                            $('p.validateTips').text('');
                                            $.ajax({
                                                type: 'POST',
                                                url: 'FitbookContest.aspx/InsertUsersItem',
                                                contentType: 'application/json; charset=utf-8',
                                                dataType: 'json',
                                                data: " + "\"{'email':'" + javacriptVariable  + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}\"," +
            " success: function (data) { " +
            "console.log(EmailId); " +
            "$('#lblErrorMessage').text(''); " +
            "document.getElementById('chkTermsAndConditions').checked = false; " +
            "if (data.d == false) { " +
            "alert('Sorry!You can participate only once!'); " +
            "$(this).dialog('close'); " +
            "} else { " +
            "alert('Email will be sent to your Account soon!'); " +
            "$(this).dialog('close'); " +
            "} " +
            "}, " +
            "context: $(this), " +
            "}); " +
            "} " +
            "}, " +
            "Cancel: function () { " +
            "document.getElementById('chkTermsAndConditions').checked = false; " +
            "$(this).dialog('close'); " +
            "} " +
            "}, " +
            "close: function () { " +
            "$(this).dialog('close'); " +
            "} " +
            "});");

            sb.Append(" }); ");
            //ClientScript.RegisterStartupScript(this.GetType(), "popup", sb.ToString(), true);
            ScriptManager.RegisterStartupScript(this, GetType(), "modalscript", sb.ToString(), true);
        }
    }
}

我想在页面加载时打开一个弹出窗口!并且还想获取将写在弹出窗口文本框中的电子邮件。我只能使用JavaScript来检索它。但是由于代码后面的双引号,我无法将其放在"data"中,如下所示:[ data: " + "\"{'email':'" + javacriptVariable + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}\"," + ]。请帮助我。欢迎提供建议!

1个回答

0

如果你想在字符串中使用双引号,可以使用 "" 代替 \",并在字符串值之前使用 @。所以:

data: " + @"""{'email':'" + javacriptVariable  + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}""," +

[编辑] 实际上,我认为你根本不应该用双引号将它括起来:

data: {'email': $('#txtEmail').val(), 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}," +

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接