Few random development Tips-N-Tricks-1

1) Custom Page Class:- It’s always a good idea to derive your Page from a custom Base Page Class.It actually gives you a chance to capture content page load event. Similar to MasterPage, which is more suitable to store common UI related stuff, a custom common BasePage class helps in keeping any common logic at a centralized place, so that future maintainence gets easy. Few scenarios where a custom base Page class can help are —
— Say if I want to clear certain Session variables on every Page load. On a similar basis, any thing related with ViewState, QueryString, Page directive attributes can be handled here.
— Good place to load any common client-scripts — say you want to add WebService Reference to the ScriptManager class.
— If you want to handle errors/exceptions on Page level. Here after exception handling make sure you call Server.ClearError() to stop the exception to propate further to global.asax/Application_Error handler or web.Config/CustomError section.

2) User Controls:- Among other so many advantages of using UserControls, I found 2 more.
— UserControls makes it very easy to use ASP.Net caching.
— During deployment, it gets easy to create a dll out of all the usercontrols.
Ideally an aspx page should not be heavily loaded with code, — instead — it should call bunch of relevant UserControls.
All business logic/data logic should be divided into distinct UserControls to make it ideal for reuse & a centralized place for future changes/modifications.

3) System.Text.RegularExpressions:-
   In addition being used for validation, regex makes it very easy to do any kind of string operations.
   Say in a multiline textbox , you want to figure out if the user hit enter key (carriage return-line feed) 4 times. regex comes very handy. Here’s an example

string x = txtMultiline.Replace("\n", "~");
 Regex regex = new Regex("~", RegexOptions.Compiled);
 MatchCollection matches = regex.Matches(x);
 if (matches.Count > 4)
     int nIndex = matches[4 - 1].Index;
     string x2 = x.Substring(nIndex + 1);
     string x3 = x.Substring(0, nIndex);

4) JTemplate :- A big advantage of using jTemplate is —  you can fetch data once — & populate many sections of the page using different jTemplates, at the same point of time. A rough psuedo code would be —

<script language="javascript" type="text/javascript">
   type: "POST",
   url: "./Default.aspx/getCustomerDetails",
   data: "{}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function (data) {
   // Populate some div1 with Template1
   $('#div1').setTemplateURL('JTemplates/Template1.htm', [], { filter_data: false });
   // Populate div2 with Template2
   $('#div2').setTemplateURL('JTemplates/Template2.htm', [], { filter_data: false });

This entry was posted in Tips-n-Tricks. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s