0

I have a piece of javascript code evaluated at runtime, with the <%= %> syntax...

Now, inside the <%= %>, instead of a hard coded string, i'd like to have the value stored in a variable... How can I do this?

This is the function:

function updateDescriptionLabel(msgToParse, dataColumn, TextBoxID) {
    var myData = JSON.parse(msgToParse.d);
    alert(msgToParse);
    alert(dataColumn);
    alert(TextBoxID);

    // this is the explicit call, it's ok
    $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));

    // NOW i want to make the call by using the variable value
    var txtDes = TextBoxID;

    $('#' + '<%= this.' + txtDes.toString() + '.ClientID %>').val($.trim(myData["DescrMacch"]));

    // BUT i get the error: Too many characters in character literal
}

============== EDIT ===============

I have a bunch of TextBoxID that, on lost focus, get a get a value from database, and display it on the appropriate TextBoxDESCRIPTION related to the ID... But I have to duplicate the code for each TextBox, so I'd like to generalize it...

I post the entire code.

 <script language="javascript" type="text/javascript">



    /* ==> JSON  */

    //Ajax Request
    function SendAjaxRequest(urlMethod, jsonData, returnFunction) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: urlMethod,
            data: jsonData,
            dataType: "json",
            success: function (msg) {
                // Do something interesting here.
                if (msg != null) {
                    returnFunction(msg);
                }
            },
            error: function (xhr, status, error) {
                // Boil the ASP.NET AJAX error down to JSON.
                var err = eval("(" + xhr.responseText + ")");

                // Display the specific error raised by the server
                alert(err.Message);
            }
        });
    }

    // I'd like to generalize it ...
    function SendComplexAjaxRequest(urlMethod, jsonData, returnFunction, dataColumn, TextBoxID) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: urlMethod,
            data: jsonData,
            dataType: "json",
            success: function (msg) {
                // Do something interesting here.
                if (msg != null) {
                    returnFunction(msg, dataColumn, TextBoxID);
                }
            },
            error: function (xhr, status, error) {
                // Boil the ASP.NET AJAX error down to JSON.
                var err = eval("(" + xhr.responseText + ")");

                // Display the specific error raised by the server
                alert(err.Message);
            }
        });
    }




// ONE function for each textBox
    function callUpdateGuastoAttributes(code) {
        var urlMethod = '<% = ResolveClientUrl("~/Services/ws_Attributes.asmx/OdLGetMacchinaAttributes") %>';
        var jsonData = "{'COD_MACCHINA':'" + code + "'}";
        var successFunction = updateLabelsGuastoAttributs;
        SendAjaxRequest(urlMethod, jsonData, successFunction);
    }

    function callUpdateCausaGuastoAttributes(code) {
        var urlMethod = '<% = ResolveClientUrl("~/Services/ws_Attributes.asmx/OdLGetMacchinaAttributes") %>';
        var jsonData = "{'COD_MACCHINA':'" + code + "'}";
        var successFunction = updateLabelsCausaGuastoAttributs;
        SendAjaxRequest(urlMethod, jsonData, successFunction);
    }


// I can have only one function:
    function callUpdateMacchinaAttributes(code) {
        var urlMethod = '<% = ResolveClientUrl("~/Services/ws_Attributes.asmx/OdLGetMacchinaAttributes") %>';
        var jsonData = "{'COD_MACCHINA':'" + code + "'}";
        var successFunction = updateDescriptionLabel;
        SendComplexAjaxRequest(urlMethod, jsonData, successFunction, 'DescrMacchina', 'TextBoxDES_MACCHINA');
    }





    /* <== CALLBACK  */
function updateLabelsMacchinaAttributs(msg) {
        var myData = JSON.parse(msg.d);
        $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));
    }

    function updateLabelsGuastoAttributs(msg) {
        var myData = JSON.parse(msg.d);
        $('#' + '<%= this.TextBoxDES_GUASTO.ClientID  %>').val($.trim(myData["description"]));
    }

    function updateLabelsCausaGuastoAttributs(msg) {
        var myData = JSON.parse(msg.d);
        $('#' + '<%= this.TextBoxDES_CAUSA_GUASTO.ClientID  %>').val($.trim(myData["description"]));
    }

// BUT I have to generalize it...
function updateDescriptionLabel(msgToParse, dataColumn, TextBoxID) {
        var myData = JSON.parse(msgToParse.d);
        alert(msgToParse);
        alert(dataColumn);
        alert(TextBoxID);

        // this is the explicit call
        $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));

        // i want to make the call by using the variables values
        var dCol = dataColumn;
        var txtDes = TextBoxID;

        $('#' + '<%= this.' + txtDes.toString() + '.ClientID %>').val($.trim(myData[dCol]));

        // i get the error: Too many characters in character literal
    }
spiderman
  • 1,565
  • 2
  • 16
  • 37
  • You don't appear to be using `eval` there. What gives you the error? Is it a JSP error or a JavaScript error? If it is a JSP error, whey are you showing is the JSP? Show us the generated JavaScript instead. – Quentin Apr 23 '13 at 09:30
  • 2
    You could not make this : `<%= %>` blocks are evaluated by the server as javascript code is evaluated by the client and then after the server as returned the page with server blocks already evaluated!!! – Samuel Caillerie Apr 23 '13 at 09:31
  • Have you seen the generated source html? – shyam Apr 23 '13 at 09:31
  • @Quentin no, the error is here why the OP could not evaluate dynamically from javascript depending on the value of `TextBoxID`, the server block... – Samuel Caillerie Apr 23 '13 at 09:32
  • @SamuelCaillerie — Ah, I see! – Quentin Apr 23 '13 at 09:34
  • 1
    possible duplicate of [Reference: Why does the PHP code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript-not-work) (Yes, that is a PHP, this is JSP, the problem and the solutions are the same though). – Quentin Apr 23 '13 at 09:35

1 Answers1

0

I propose such solution (depending on your other code...) :

function updateDescriptionLabel(msgToParse, dataColumn, TextBoxID) {
    var myData = JSON.parse(msgToParse.d);
    alert(msgToParse);
    alert(dataColumn);
    alert(TextBoxID);

    // this is the explicit call, it's ok
    $('#' + '<%= this.TextBoxDES_MACCHINA.ClientID  %>').val($.trim(myData["DescrMacchina"]));

    // NOW i want to make the call by using the variable value

    $('#' + TextBoxID).val($.trim(myData["DescrMacch"]));
}

and then when you call this function do this :

updateDescriptionLabel("your message", <yourcolumn>, <%= this.TextBoxDES_MACCHINA.ClientID  %>);
Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
  • @AndreaRossi ok, let me know if there is any issue... The aim here is to have the server blocks pre-fill before javascript is loaded. Another way could be to prefill javascript variables with these values : `var clientIdDES = <%= this.... %>`. – Samuel Caillerie Apr 23 '13 at 09:56