Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Tuesday, December 25, 2007

Valid Reasons to Create a Routine

"Valid Reasons to Create a Routine", I've been trying to make the list of the reasons for few years, because 4 years ago I teach programming at my University. But it seems I can't explain it to myself either. Yeah of course I know (some of)the uses of routine, it just I can't put it into words.

Until lately, I read this sentence from title of a sub-chapter on a book called "Code Complete" by Steve McConnell. Currently I read only 170 pages from 850 pages of the book, but no doubt, this is one of the best book I ever read. If you are a programmer, you better read this book. :)

Here's the list that the book wrote:
- Reduce complexity
- Avoid duplicate code
- Support sub classing
- Hide sequences
- Hide pointer operations
- Improve portability
- Simplify complicated boolean tests
- Improve performance

There's description for every point stated above in the book, and I couldn't agree more with the list. So buy the BOOK. Thanks Steve for the knowledge. :)



Thursday, December 20, 2007

First Post and First Problem

How do you code to open a new window when a button is clicked? If you use HTML or server side scripting like ASP, it's pretty simple, because you only need to call a javascript function window.open(...) in the button's onclick event. Thing gets a bit more complicated if you code using ASP.Net, which I experienced lately. In my case, I want the popup window to display a particular page according to selected value of a dropdownlist.

So, after doing some little research in Google, I tried to use Response.Write() method and put "<script>window.open(...)</script>" as its param. The Response.Write () was called inside the OnClick event handler of the button.
Note: I was using code behind class.
Since the popup page should be dependent on the selected value of a dropdownlist, so I used a switch-case statement to determine which page I should open. Actually it worked, but there was a small thing that bother me. After I clicked the button a popup window was displayed, and the main page was also refreshed. This refresh made the layout of the main page in a mess.

Then, I tried to use another method, that was ClientScript.RegisterStartupScript() and used window.open function as one of its param. This method worked perfectly fine for me. A popup window was displayed and the main page was still refreshed (of course!) but didn't ruin the layout.

"<script>window.open(...)</script>"I guess it was because the difference of where ASP.Net put javascript function in the page. Using the first way, the javascript function is printed on the very first line of the HTML code, while the second way, it prints javascript function inside "body" tag after the button tag.