I have a JSON object that I need to pass from the server to the client side to save the data off in a database.
I can sucessfully pass it using:
var str2 = JSON.stringify({
o: oJSON
});
$.ajax({
type: "POST",
url: "WebForm1.aspx/Test",
data: str2,
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {},
error: function (e) {
alert(e);
}
});
and
<System.Web.Services.WebMethodAttribute(),
System.Web.Script.Services.ScriptMethodAttribute()> _
Public Shared Function Test(ByVal o As RootObject) As String
Dim strName as string = textboxName.text
End Function
I have to save two different sets of data off. The first set has to be saved before the json object can be saved. It is justing saving textbox values to the database. The second set is storing the json to the database.
I am trying to set sName to equal the textbox value and am getting the following error:
Cannot refer to an instance member of a class from within a shared
method or shared member initializer without an explicit instance of
the class
Is there another way to pass the JSON object to the client side where I can access the textbox values on the page ?
Or am I going to have to include Name along with the other values that I need in the JSON object ?
No, you won’t be able to access your Page’s TextBox controls from the static/Shared method. In order for them to have access to that, the AJAX call would have needed to pass the entire ViewState back and run the Page through its life cycle in order to instantiate the TextBox and determine what its value should be. At that point, you’d basically have the UpdatePanel, and would have lost the performance advantages of using page methods.