views:

517

answers:

2

hi, i am really confused here, as i read many places, Update panel makes a full post back, and i have somehow understood that web serivces are much much better for performance, so if i am developing my site should i user web services or normal functions like the following

   protected void Page_Load(object sender, EventArgs e)
{
  GetDate();
}
protected void Button1_Click(object sender, EventArgs e)
{

}
[WebMethod()]
protected DateTime GetDate()
{
    return DateTime.Now;
}
  • will i use post back ?
  • what are the pitfalls of web services, specially in AJAX website ?
  • what is your advise on best squeezing out the most performance with Microsoft AJAX

thanks in advanced.

+1  A: 

Personally, I'd be looking at using jQuery to invoke an ASP.NET MVC action; much simpler pipeline, and you know exactly what is going up and down the wire.

Marc Gravell
+1  A: 

web services get you around the whole viewstate mess for one.
But yes, I consider using web services as a best practice for getting the most bang for your buck in the web world.

Avoid Post back/Call back whenever you can.

Pitfalls: when creating a web service, you have to pass all of the data you would normally have via ViewState.

I would highly recommend leaning JQuery. It will help out a lot.

As for web service performance: use JSON (ScriptMethod) instead of XML to move data back and forth.

Edit: about ViewState mess. If you are not careful the ViewState (used to serialize controls, and is a hidden field on the page) can quickly grow very large. This data is sent from the browser to the server for every post-back -- but not for web services. That can make web service calls significantly faster than postbacks (for pages with large ViewStates). ViewState is not evil, but you want to keep an eye on it.

Postbacks can occur anywhere a transition between a web control with runat='server' and code executing on the server. For example: button click events cause a postback.

Asp.Net developers favor postbacks because they are easy to implement (the IDE hooks them up for you).
Web services are slightly harder and require some (small) knowledge of JavaScript.

Another simple option is to use UpdatePanels. But the still utilize ViewState.

Chris Brandsma
man,- regarding viewstate mess, can you elaborate more ?- in my simple sample how can i avoid postback ?
I added more content to the post. I hope that helps.
Chris Brandsma
man great great answer, if it is not a hassle can you convert my sample to a none post back one ?