views:

79

answers:

4

Hi,

I am using the web kit browsers local database to temporarily store some data and when I want to access it I create an object

function patientSelectHandler(transaction, results) {
        var row = results.rows.item(0);
        var patient = new Object();

        patient.name = row['name']
        patient.dob = row['dob']
        patient.gender = row['gender']
}

Is there a way to access this object from code behind, without having to populate textfields/labels/dropdowns and then get the values from there?

example as it is now:

function patientSelectHandler(transaction, results) {
        var row = results.rows.item(0);
        var patient = new Object();

        patient.name = row['name']
        patient.dob = row['dob']
        patient.gender = row['gender']


        $('#txtPatientName').val(patient.name);
        $('#txtDOB').val(patient.dob);
    $('#ddlGender').val(patient.gender);
}

edit:

Updating my example a bit:

    var patientString = JSON.stringify(patient);
    var inputField = $("<input type='text' name='hiddenField" + i + "' id='hiddenField" + i + "'></input>");
    $('#Patients').append(inputField);
    $('#hiddenField' + i).val(patientString);

and then a loop in the code behind

            for (int i = 0; i <= count; i++)
            {
                string n = String.Format("{0}", Request.Form["hiddenField" + i]).ToString();
                JObject o = JObject.Parse(n);
                string name = (string)o["name"];
//now I can get all values into variables and use them when calling the web service
}
+1  A: 

If you have many fields to send, you can JSON encode everything and put it into a single multiline text field (textarea). Then you can easily decode it on the server.

idealmachine
I have about 50 elements where I enter data and as it is now I insert it into a remote database and it's working. However, if I let's say have 10 rows in the browser database I have to populate the fields, click save, populate the fields, click save and so on. I Wish to take all the rows in the local database and send them to the server with one click if it's possible.
Morgan
+3  A: 

You don't need to set it to textfields for any reason...

I would probably do something like

var patientString = JSON.stringify(patient);
$('#myHiddenInput').val(patientString);

Otherwise, depending on the flow of your application, you can post that object in string form to the server using AJAX.

Then, you will have to use a method to parse that string back into object formation. I'm not to familiar with c# but i'm sure it would be easy to find or write such a method.

Andy Groff
Since I have many rows, is it possible to create a new hidden textfield for each row, set the id to #hiddenInput and the row number (i) and then set the value to patientString? If it is I guess I have to click on a button 'Get all Customers' and then click another one 'synchronise' to do what I want?
Morgan
So I did that with var patientString = JSON.stringify(patient); var inputField = $("<input type='text'></input>").attr('id', "hiddenField" + i); $('#Patients').append(inputField); $('#hiddenField' + i).val(patientString);
Morgan
Sorry... I didn't see your response. So you got it working? That way sounds like it would work fine. Another option is to loop through all of your rows and append each patients object to annother object which stores all the patients. Then you can stringify that object and set it to an input... but whatever works :-)
Andy Groff
A: 

Keep in mind -

  1. When your server code runs, it builds up a page object that it then uses to generate html to send to the browser. Once the html is generated that page object is destroyed. By the time the browser shows your page, your server resources don't exist any more.
  2. When the browser submits a request for a page, it destroys the DOM for whatever page it's showing. So by the time your server starts, your javascript doesn't exist any more.

Thus, the two systems are normally completely separate. You can get around this by passing ajax json messages between client and server.

Joel Coehoorn
Thanks for the comments
Morgan
A: 

I would use AJAX post to store your JSON object on the server.

var str = JSON.stringify(...);

$.ajax({

    type: "POST",

    contentType: "application/json; charset=utf-8",

    url: "Default.aspx/validate",

    data: str,

    error: function(XMLHttpRequest, textStatus, errorThrown) {

        ...
    },

    success: ajaxCallback,

    dataType: "json"

});

And on the server

[WebMethod]
public static string validate(Dictionary<string, object> patient)
{
    ...
    //return your JSON response
}

And then just iterate through Dictionary object on the server (key - object parameter, value - it's value).

negative