Efficiency Question for an Ajax App
Currently I am dealing with a web application which uses a txt file as a database for testing for now. But we will connect it to a server later on.
My question is, if there is a more efficient way to get my obj开发者_开发问答ects than the way I am using now.
During the page_init I am getting all my objects into a Collection as List-<-TravelP->-, then I am populating the ajax toolkit accordion objects in the page with that. They are also firing some server side events of some hidden buttons.
I have some client side buttons which fires callbacks for getting some other objects to populate the accordions in an update panel.
And I am using .net Collections too much like dictionary and list, I am wondering if using arrays is more efficient.
Could you advise me about how to make this site better and faster ?
Is it better or possible to initialize those TravelP objects in javascript at the beginning and use it like that ?
Any comments would be greatly appreciated, Thanks
First of all, I'd loose the UpdatePanels. If performance is what you're after, forget about UpdatePanels.
Instead, you can write ScriptService web services (in C#) and call them from your JavaScript code. These special web services can return .NET objects which then, can be used in JavaScript code (make sure they are serializable though. Otherwise you'll need to write your own JavaScript serializer).
I'd start with that. Doing so will give you the initial performance boost. Afterwards you can think of more to do to enhance your code.
You might want to look into using jQuery for a client side JavaScript library. It makes it pretty easy to pull data from ASMX/WCF services created in .NET. You can return the data in a variety of ways XML/JSON and easily manipulate the page using jQuery DOM selectors!
Here is a simple call to load some JSON data via ASMX service.
$.ajax({
type: "POST",
url: "demo.asmx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{q:'a', limit: 10}", // Passing params to function!
success: function(res) {
// Do your work here.
// Remember, the results for a ASMX Web Service are wrapped
// within the key "d" by default. e.g. {"d" : "Hello World"}
}
});
And here is an example ASMX service returning a LIST, which really could have been anything. If you already have strongly typed POCO objects, you can probably probably already serials your data using a similar method. There is also the JSON.NET library that will allow you to serialize an entire DataTable.
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class Autocomplete : System.Web.Services.WebService {
[WebMethod]
public List<string> GetData(string q, int limit)
{
// A collection to hold our results
List<string> customers = new List<string>();
// Our source of names, could be a DB query
string[] db = new string[]{"Amy","Betty","Brent","Fred","Mark","Sally","Sam","Richard","Wilma","Yolanda","Zack"};
// Looping through the datasource to select the items that match
foreach(string cust in db)
{
if(cust.ToLower().Contains(q.ToLower()))
{
customers.Add(cust);
}
}
// Sort the list
customers.Sort();
// Return the items that contained the text in alphabetical order
return customers;
}
}
Lots of things you can do and there are lots of people writing about using these technologies. If you just starting, you should probably look at WCF since it's the successor to ASMX services!
精彩评论