从后面的代码中检索javascript var值

时间:2013-02-19 04:24:46

标签: c# javascript c#-4.0 jquery

我的整个javascript都在代码背后。我想为代码背后的ajax数据检索javascript var EmailId值。但是因为我必须用双引号写它,它会导致很多问题。这是我的代码,

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检索它。但由于代码背后的双引号,我无法在数据&#34;数据&#34;作为[ data: " + "\"{'email':'" + javacriptVariable + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}\"," + ].请任何人帮助我。建议欢迎!

1 个答案:

答案 0 :(得分:0)

如果你想在字符串中使用双引号,请使用&#34;&#34;而不是\&#34;,并在字符串封闭值之前使用@。所以:

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

[编辑]实际上,在我看来,你根本不应该用双引号括起来:

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