Search code examples
javascripttelerik-grid

I want to retrive old value, not new, from textbox in RadGrid using Javascript


I have textbox(txtCustomerID) in RadGrid with value "suraj". When i click on checkbox then is enable and that time I am edit this with "pawan" but again I am click on checkbox that time I want retrive my old value like "suraj" not "pawan".

Below is my checkbox change function:

 function rdoNewCustomer_OnClientCheckedChanged(sender, args) {

        var checked = sender.get_checked();
        var grid = $find("<%=rgvMultiContractAccept.ClientID %>");
        var masterTableView = grid.get_masterTableView();
        var index = sender.get_commandArgument();
        var row = masterTableView.get_dataItems()[index];

        if (checked == true) {

            row.findControl("txtCustomerID").enable(true);
            row.findControl("rdoAnotherJob").set_checked(false);


        }
        else {


            row.findControl("txtCustomerID").disable(true);
        }
    }

Solution

  • You can use the data-* attributes to store the original value of the textbox.

    When the textbox is enabled you store the old value to the data-value attribute and when it is disabled you can retrieve the old value from the attribute and reset it to the textbox.

    Here is the updated function:

    function rdoNewCustomer_OnClientCheckedChanged(sender, args) {
        checked = sender.get_checked();
        grid = $find("<%=rgvMultiContractAccept.ClientID %>");
        masterTableView = grid.get_masterTableView();
        index = sender.get_commandArgument();
        row = masterTableView.get_dataItems()[index];
    
        if (checked == true) {
            row.findControl("txtCustomerID").enable(true);
            findControl("rdoAnotherJob").set_checked(false);
            var jTextBox = jQuery(row.findControl("txtCustomerID"));
            // Save the original value to the data-value attribute
            jTextBox.attr('data-value', jTextBox.val());
        }
        else {
            var jTextBox = jQuery(row.findControl("txtCustomerID"));
            // Set the original value from the data-value attribute
            jTextBox.val(jTextBox.attr('data-value'));
            row.findControl("txtCustomerID").disable(true);
        }
    }