All I'm wanting to do开发者_StackOverflow中文版 is pass a JS array to a VB .net code behind method via a AJAX call or other way if there is another way? Can someone point me in the right direction?
I'm basically wanting to save the values in the JS array to a database.
You could use a HiddenField(runat=server) to store that value f.e. as comma-separated values(array.join()). Its value is stored in the Viewstate.
Dim myJavaArray As Object, myValue As Variant, myArrayIdx As Long
Set myJavaArray = SomeExistingJavaArrayObject
' you can call .length like this because it is a property, not a method
For myArrayIdx = 0 to myJavaArray.length step 1
myValue = CallByName(myJavaArray, CStr(myArrayIdx), VbGet)
' do something with myValue here
Next
You could also use a PageMethod. It's a public static method in your page class, that has a [WebMethod]
attribute. In your ScriptManager, you can do EnablePageMethods="true"
, and you'll be able to call your page method directly from javascript, and it will bypass the normal asp.net page lifecycle.
In your page class code behind file: (sorry, don't know VB, so C# it is)
[WebMethod]
public static void SaveValues(string[] vals)
{
// Save vals to database
}
Your ScriptManager:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
And in your javascript:
function save()
{
var values = ["Value1", "Value2", "Value3"];
var userContext = null; // You can use this for whatever you want, or leave it out
PageMethods.SaveValues(values, save_success, save_error, userContext);
}
save_success
and save_error
would be your success and error callbacks. userContext
can be anything you want. You could also just define the success callback inline, using a closure:
function save()
{
var values = ["Value1", "Value2", "Value3"];
var userContext = null; // You can use this for whatever you want, or leave it out
PageMethods.SaveValues(values, function(result) {
alert("Values have been saved");
},
function(err) {
alert("Error");
});
}
It's also possible to call your PageMethods directly using jquery: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Serialization/deserialization between javascript and .NET types is done for you automatically (by the .NET JavaScriptSerializer).
PageMethods don't get a lot of love in the spotlight, but they are very useful. You don't go through the whole asp.net page lifecycle (Page_Load, Page_LoadComplete, etc), which reduces overhead considerably (and saves you from weird bugs when you use things like Page.RegisterStartupScript), yet you don't have to create a full blown web service or REST service to make simple calls right from client code (javascript). Far superior to the mess of update panels, in my opinion.
However, if you find that you're using them all over the place, then you might not be properly "separating your concerns" in your application design. A separate service layer might be in order. I've really been enjoying WCF services, as I can add both RESTful and SOAP endpoints to a single service, and call the methods directly from client code (using the REST endpoint) OR in a code behind file (using the SOAP endpoint via Add Service Reference).
Just food for thought.
I use the approach described by Tim, but use also jQuery and the Json Library from this Page and parse the array with this code:
var strArray = JSON.stringify(array);
$("#<%=hidField.ClientID %>").val(strArray);
精彩评论