Get javascript var value from code

All my javascript is behind the code. I want to get javascript var EmailId value for my ajax data by code. But since I have to write this in double quotes, it causes a ton of problems. Here is my code,

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

      

I want to open a popup on the download page! and also want to receive an email to be written in the popup text box. I can only get it using javascript. But due to the double quotes on the code behind, I cannot get it to "data" like [ data: " + "\"{'email':'" + javacriptVariable + "', 'ItemId':'" + Convert.ToString(Session["ItemID"]) + "'}\"," + ].

Please help me. Suggestions are welcome!

+3


source to share


1 answer


If you want to use double quotes in a string, use "instead of \" and use @ before the string that encloses the value. So:

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

      



[Edit] Actually, it seems to me that you shouldn't enclose it in double quotes at all:

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

      

0


source







All Articles