.Net, ASP.Net, C#, VB.net, SQL Server, Xml, CSS, Design Patterns related tips, tricks, code snippets, articles, links, thoughts, etc. from Binu & Subi Thayamkery.

Binu Thayamkery is a seasoned software architect with more than 13 years of experience in developing enterprise grade connected systems using Microsoft Technologies. In his current position as a lead consultant-solution architect with Prudential Financial, he is working on architecture of next generation investment reporting framework using .net 3.5/WCF/AJAX, etc. He holds a Masters Degree in Computer Science from Colorado State University. Subi Thayamkery is an experienced software developer with more than 8 years of developing various application software systems ranging from workflow automation systems to compliance management tools. She currently works as a technology consultant for Prudential Financial where she helps develop a new system for corportate governance department. She holds an Electrical Engineering degree from New Jersey Institute of Technology.

Tuesday, March 28, 2006

How to find out which webcontrol generated postback?

There will be instances where this information could come handy. You are doing some action specific to a control on postback and don' t want that to happen when postback happens because of some other control. Trick to figure out this is to understand how ASP.Net postback works. When ASP.Net renders HTML, it creates a block
of Javscript that looks like this,

1 function __doPostBack(eventTarget, eventArgument)
2 {
3 var theform = document.Form1;
4 theform.__EVENTTARGET.value = eventTarget;
5 theform.__EVENTARGUMENT.value = eventArgument;
6 Form1.submit();
7 }

When any controls generates a postback (example: Button Click), this Javscript is called and values are set to the hidden variables named __EVENTTARGET and __EVENTARGUMENT. __EVENTTARGET is the control that is doing the postback and __EVENTARGUMENT holds any additional information for that event. Now, with this information, it is easily understood how to use this at server side Page_Load event to determine which control caused the postback.

This is done by examining the request obejct to see the values in __EVENTTARGET.

1 char[] delim = {'_'};
2 string[] pbCtrl = Request.Form["__EVENTTARGET"].Split(delim);
3 System.Diagnostics.Debug.WriteLine (pbCtrl[0]);
4

pbCtrl[0] should contain the ID of the control that triggered this postback!


Cheers!

Friday, March 03, 2006

ASP.Net - Some performance myths.

Here is some performance myths in .net world as explained by Rob Howard in an article in MSDN Magazine along with what we think!

  • C# code is faster than Visual Basic code - False! Rob says that there is a grain of truth in this mainly because VB.Net allows performance hindering actions like not explicitly declaring types.I think both should be comparable because all the .net languages are ultimately creatingt code targeting the framework CLR and CTS. If you follow good programming practices, VB.Net and C# should provide with same features and performance (pretty much!)

  • Code-Behind is faster than Inlince code - False! Rob points out that sometimes it is advantageous to just change inline (or add inline) code because touching code behind means a Dll rebuild. If you ask me, I would say NO to inline code, haven't we had enough of spaghetti coding with all than classic ASP?

  • Components are faster than Pages- False! True in classic ASP, no longer in ASP.Net, since page is also a class, it provides the same performance as components. Again from a modular design point of view, lets go with components!



Cheers!

Wednesday, March 01, 2006

Clear your web applications Cache

We all know that Cache class provides an implementation for cache for a web application in ASP.net. Cache provides fast access to frequently used data. You can add your not so frequently changed data to Cache for a speedy access. Cache has an application scope, what ever is in cache is used by all users of your web application.

Being said that, I find sometimes a need to clear my web applications cache without affecting anything. In order to do that, this is the trick I have come up with (nothing genius here :)

I created a secure page (admin login required) with a "Clear All Cache" Button,
and here is what I do in that button click!



private void ClearCache_Click(object sender, System.EventArgs e)
{
IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
string cacheKey="";
while (CacheEnum.MoveNext())
{
cacheKey = Convert.ToString(CacheEnum.Key);
Cache.Remove(cacheKey);
}

}