Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries for the ‘asp.net’ Category

jQuery script to scroll to the top of window

$('html, body').animate({ scrollTop: 0 }, 'slow');

Call web service via jQuery in asp.net

step 1. Add Web service, and you must add a [System.Web.Script.Services.ScriptService] to the service class

(in webservice.asmx.cs file)

   1: /// <summary>

   2: /// Summary description for WebService1

   3: /// </summary>

   4: [WebService(Namespace = "http://tempuri.org/")]

   5: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   6: [System.ComponentModel.ToolboxItem(false)]

   7: // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

   8: [System.Web.Script.Services.ScriptService]

   9: public class WebService1 : System.Web.Services.WebService

  10: {

  11:     [WebMethod]

  12:     public string DeleteProduct(string id)

  13:     {

  14:         Thread.Sleep(800);

  15:         return "Hello World22: " + id;

  16:     }

  17: }

step 2. call webservice in aspx via jQuery

(in head of the page)

   1: <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

   2: <script type="text/javascript">

   3:     $(function () {

   4:         $('a').click(function () {

   5:             deleteProduct(111);

   6:         }); // end of  $('a').click(function () {

   7:  

   8:     });     // end of $(function () {

   9:  

  10:  

  11:  

  12:     function deleteProduct(id) {

  13:         $.ajax({

  14:             type: "POST",

  15:             url: "WebService1.asmx/DeleteProduct",

  16:             data: "{id: '" + id.toString() + "'}",

  17:             contentType: "application/json; charset=utf-8",

  18:             dataType: "json",

  19:             success: function (msg) {

  20:                 AjaxSucceeded(msg);

  21:             },

  22:             error: AjaxFailed

  23:         });

  24:     }

  25:     function AjaxSucceeded(result) {

  26:         alert(result.d);

  27:     }

  28:     function AjaxFailed(result) {

  29:         alert(result.status + ' - ' + result.statusText);

  30:     }  

  31: </script>

Use HttpRuntime.Cache to call asp.net Cache

In some scenario, you can’t visit asp.net cache, even via HttpContext.Current.Cache. For example, in Unit Test mode. In this case, you can use the HttpRuntime.Cache to access asp.net Cache.

Page lifecycle events in asp.net – SILVER-U

You may think it is hard to remember the page life cycle events in asp.net, remember ‘SILVER-U’, they are

S: Start
I: Initialize
L: Load
V: Validate
E: Event handling
R: Render
U: Unload

Globalization and Localization in ASP.NET

Internationalization involves Globalization and Localization. And the globalization is the process of designing applications that support different cultures. While localization is the process of customizing an application for a given culture.

The format for the culture name is "<languagecode2>-<country/regioncode2>", where <languagecode2> is the language code and <country/regioncode2> is the subculture code. Examples include es-CL for Spanish (Chile) and en-US for English (United States).

ASP.NET keeps track of two culture values, the Culture and UICulture. The culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting. The UICulture determines which resources are to be loaded for the page by the ResourceManager. The ResourceManager simply looks up culture-specific resources that is determined by CurrentUICulture. Every thread in .NET has CurrentCulture and CurrentUICulture objects. So ASP.NET inspects these values when rendering culture-dependent functions. For example, if current thread’s culture (CurrentCulture) is set to "en-US" (English, United States), DateTime.Now.ToLongDateString() shows "Saturday, January 08, 2011", but if CurrentCulture is set to "es-CL" (Spanish, Chile) the result will be "sábado, 08 de enero de 2011".

Performance tuning tips for ASP.NET and IIS 7

1. Browser caching

In part 1, we looked at how it was possible to set an expiration header to any static file such as JavaScript and CSS files, so the browser would cache them for a long time and thereby optimize both for bandwidth and the number of requested files going from server to browser.

The problem with setting a browser cache expiration date of i.e. a JavaScript file to a year in the future becomes clear when you change the file before it expires in your visitor’s browsers. They simply won’t see the changes until they either clear their cache or hits F5 manually.

2. Bundle multiple files

Another common website performance issue is that there are many JavaScript and CSS files included on a page. This scenario results in the browser have to download a lot of extra files and that all slows down the performance of a website. The solution to this is also very simple when you’ve first completed the above steps to register the HTTP handler in web.config and called the BundleHelper.InsertFile method when inserting JavaScript and CSS files.

3. HTTP compression

You’ve always been able to perform HTTP compression in ASP.NET by using third-party libraries or own custom built ones. With IIS 7 you can now throw that away and utilize the build-in compression available from the web.config. Add the following line to enable HTTP compression:

<urlCompression doDynamicCompression=”true” doStaticCompression=”true” dynamicCompressionBeforeCache=”true”/>

By default, only text based content types are compressed.

4. Cache static files

To speed up the load time for the visitors, it is crucial that everything that can be cached by the browser IS cached by the browser. That includes static files such as images, stylesheets and script files. By letting the browser cache all these files means it doesn’t need to request them again for the duration of the cache period. That saves you and your visitors a lot of bandwidth and makes the page load faster. A well primed browser cache also triggers the load and DOMContentLoaded event sooner.

It’s worth noticing that the output caching respects file changes and therefore refreshes ever time changes are made to the JavaScript and CSS files tunnelled through this code.