Thursday, September 17, 2009

Avoid numbers in Textbox using ASP.NET AJAX

An old question which was recently asked for an interview.

How can you prevent numbers from being entered into a Textbox, using Microsoft ASP.NET AJAX Framework. Thinking about UpdatePanel’s? Be clever, I have some client-side codes, using the framework itself.


   1: <script type="text/javascript">
   2:     function pageLoad() {
   3:         var tbox = $get("Text1");
   4:         $addHandler(tbox, 'keypress', text1_keypress);
   5:     }
   7:     function text1_keypress(e) {
   8:         var code = e.charCode;
   9:         if (code >= 48 && code <= 57)
  10:             e.preventDefault();
  11:     }
  13:     function pageUnload() {
  14:         var tbox = $get("Text1");
  15:         $removeHandler(tbox, 'keypress', text1_keypress);
  16:     }
  17: </script>


   1: <form id="form1" runat="server">
   2: <asp:ScriptManager ID="ScriptManager1" runat="server">
   3: </asp:ScriptManager>
   4: <div>
   5:     Enter text value:
   6:     <input id="Text1" type="text" />
   7: </div>
   8: </form>


What are Mashups?

It’s been a long time, I’ve been hearing about mashups in the web world. Surprisingly, I came across the same when I’s reading Manning’s ASP.NET AJAX in Action.

A mashup is a web application that consumes content from more than
one external source and aggregates it into a seamless, interactive experience
for the user.

If you ask me a live example, here it is PageFlakes.

Tuesday, September 15, 2009

Exiting an Application on Error

Error handling is always a nightmare for every programmer. Because, testing you application in your development lab and exposing the application to a real-world lies in two extremities. The very reason where everyone go for alpha/beta and finally the RC release. Hey…Hey…I’m not going into core-concepts.

Recently, I’s re-optimizing a 6-month old c#-code (written in .NET 2.0) to .NET 3.5. Re-optimizing the code is a tough task, specially when the code was written by someone else. It deals with studying the code, understanding the logic and also good, if you can preview the test-cases encountered while re-designing.

When digging deep into the logic, I came across code-blocks where, even if you catch an Exception, you can’t continue because that might cause an abnormal exception at some other point. Huh! and I’s forced to kill the application at that point, using one-line code.

   2: Environment.Exit(1);

Remember, use this code safely. Use it only when you came across a do-or-die situation. Killing an application unnecessarily is bad way of programming.

Happy Programming.

Best viewed in Internet Explorer 8.