I have used the ASP.NET Hidden field control, allowing me to generate single-item data values in javascript and then send the values back to the server on postback.
My new problem is that I have an entire list of items that are created and edited entirely in the browser client. The control itself is not an ASP.NET control. (I’m using the kendo list view).
How do I send this data back?
I’m assuming that the javascript will need to encode the local datasource into the DOM somehow, and the server code will extract the values somehow.
The only “somehow” I can think of is to serialize all of the values into a single string and set that as a hidden field value.
Serializing the values using javascript and deserializing these using C# is certainly dooable, but seems a bit too “manual”. I’m wondering if there is a more straightforward way which doesn’t involve manual serialization. Some way to write to the DOM which is then picked up by the ASP.NET binding functionality.
For this problem, the data is in the following format:
[
{
sequenceNumber : 1,
mediaFileId: 3
},
{
sequenceNumber : 2,
mediaFileId: 28
},
{
sequenceNumber : 3,
mediaFileId: 32
}
]
You could argue that the sequenceNumber is redundant, but I will be adding new fields in the future, so it would be best to arrive at a solution which caters for multi-field data objects.
I know I could replace the entire post with an AJAX call, but at this point that would be too big a change to the existing page.
Not sure what you mean there – without an ASP.NET server control, there’s nothing that will pick up the value on the postback. The server controls use the hidden field for ViewState to store this data, but it’s not really feasible to add or update items from the client.
Since you already have your data in JSON, I would just put it into a
HiddenFieldValueand use a JSON deserializer on the C# side to get it back to a POCO.JavaScriptSerializer,DataContractJsonSerializer, orJSON.NETshould all work for the C# deserialization routine without much manual work.