Monday, March 21, 2011

A Practical Tutorial for the ASP.Net Event Model

There are likely a million guides on the web that detail the asp.net event model; however, many developers still do not grasp how to use the events properly. If you want a detailed guide to the events, try this resource. The goal of this post is to provide a guide to the actual use of the most important events; not to provide exhaustive details on everything. Also keep in mind the suggestions in this post apply most of the time; but as always, decisions have to be made on a case by case basis as your specific page may have circumstances that make it very different.



1. Page_Init



  • Populate lists, such as drop-downs

  • Set default/initial values for inputs

  • Inject content into literals

As the name implies, this event should be used for initialization of your controls. Setting default values here has several advantages. First, even if you forget to check whether the current request is a postback, your initializations will not overwrite values set by the users. Second, you get a performance boost because you do not clog up the view state. Many sources suggest using the view state to reduce database requests thereby increasing performance; however, I disagree as this is actually the duty of data-caching. One caveat to remember about Page_Init is that it cannot be used to initialize controls or populate lists when the initialization value is dependent on the value of another control. Resist the urge to set control visibility in Page_Init; it might seem to make sense at first, but will cause you trouble later.



2. Page_Load



  • Initialize controls (which might not have been visible prior to the postback) when their values are dependent on the values of other controls in the page

Page_Load is by far the least useful event on this list despite the fact that it is the event most commonly used by many developers. I cannot think of a good reason to put anything in Page_Load unless it is only used in a post back to vary page content based on user input. Resist the urge to put pretty much anything in this event. Above all else, avoid databinding in the Page_Load or you will regret it later!



3. Control Events


  • Save content changes to the database

  • Set flags on instance fields

  • Custom validation

  • Possibly redirects (try to avoid using POST requests to do redirects as it makes your site harder to use)

The control events are where data storage should be done whether it's to the database or some other medium; don't forget to check whether your page is valid. Control events are also a great place to execute business logic. Resist the urge to make UI changes in the control events (such as setting control visibility); instead set a flag and make the change in Page_PreRender. Some obvious exceptions to this rule are events like calendar DayRender or repeater DataBinding.



4. Page_PreRender



  • Set whether the control is visible

  • Set whether the control is enabled

  • Changes in content based on a varying mode or stage

  • Data-Binding!


First and foremost, this is where databinding should be triggered. This is because Page_PreRender is when your data will be in the state that you want the user to see. Prematurely triggering a databind can give users the impression that their changes did not go through. Page_PreRender is also where Visible and Enabled properties of controls should be set. Taking these steps here ensures your user gets the proper content if you're varying a page based on stage or mode in your business logic. Resist the urge to do resource cleanup in this event; that should be done in Page_Unload. If you are the type of developer that hates maintainable code and used a markup datasource, databinding can occur after the Page_PreRender event making resource clean up inappropriate here.

Wednesday, November 24, 2010

Get service of SVsBuildManagerAccessor fails

Output:
Get service of SVsBuildManagerAccessor fails

When this rather cryptic message is shown in Visual Studio 2010, it's because your one-click publish has failed (perhaps several seconds prior to your push deadline). Even worse, no webpages indexed by Google contain any mention of this obscure error.

The funny thing about Visual Studio publish is that it relies on some Internet Explorer assemblies; if one of these assemblies, specifically, ieproxy.dll, is missing or unregistered, you will get the error message "Get service of SVsBuildManagerAccessor fails" when you try to publish.

The quickest fix for this problem is to either update or reinstall Internet Explorer, alternatively, if you are sure the dll is present and you enjoy the command prompt, you can use regsvr32, to make sure ieproxy.dll is in the registry.

Credit is due to patthewebrat

Wednesday, October 6, 2010

jQueryTOOLS TOOLTIP

I was putting together a quick prototype, and decided to use jQueryTOOLS TOOLTIP. I had been looking around at different jQuery plugins the week prior and was impressed by their demo; it looks great. The good thing about the the jQueryTOOLS TOOLTIP is it is very configurable; read, "it does not look like it does in the demo out of the box so I hope you have a good graphics artist". For something as simple as a tooltip, it was also a little bit difficult to get working properly.

When I tried to use it for my table rows it was a bit choppy when you moused over, also if I did not set the "tip" option, it turned my row into the tooltip (after tearing the row out of the table permanently). For something as simple as a tooltip, if I was looking for a high level of configuration I would just make it myself; I want simple and works within seconds. I will not be using jQueryTOOLS TOOLTIP in any production applications.

Saturday, October 2, 2010

"class" is a Reserved Word in Internet Explorer

I'm a big fan of most of Microsoft's products, even Vista; but I loath Internet Explorer. If you are writing JavaScript make sure you avoid naming variables "class" as it is a reserved word in Internet Explorer (even IE8).

Thursday, September 30, 2010

Entity Framework Web Application Deployment

In retrospect, this seem painfully obvious, but when you deploy your Entity Framework web application, I would recommend that you script your database from SQL Management Studio as opposed to selecting "Generate Database from Model..." in the Visual Studio .edmx context menu.

Yes, I made the mistake of using the model generated SQL to create my production database, but it was a very small project and I had not gone into SQL Management Studio once during coding...

Happy Coding

Friday, September 10, 2010

What I wish I read a decade ago

In the field of software development two types of developers seem to be working their hardest to destroy you application and push back your release date. The first is the "Copy and Paste Kid", the second is "Over-Generalizing Gerry". The Copy and Paste Kid has never heard of functions or constants and makes numerous humorous mistakes. Over-Generalizing Gerry is the evil "genius" who will destroy your code-base by over-engineering you application into oblivion. Watch out, he'll leave no stone unturned because he will either try to anticipate every future situation imaginable or make sure your application conforms (insert buzz word paradigm) religiously. If you let your local evil "genius" gain too much self-confidence he might even try to create his own language or language extension! Pshhh, what the heck did the creators of JavaScript know?

The frustrating thing about talking to even a mild Over-Generalizing Gerry is he will often confuse you with the Copy and Paste Kid! I've finally found an article that articulates perfectly why there is an "over" in "over-generalize". I myself have been guilty of over-engineering all too often; that's why I wish I had read this article a while ago: Tips for maintainable Java code.

The preceding article is probably the best I have ever read as far as good practices outlines go. It applies to any language, not just Java.

Friday, July 30, 2010

Making a custom container control in ASP.Net (without templates)

If you're anything like me, you've tried to make your own ASP.Net server control that had container functionality, but were greeted with this exception when parsing:

"Type 'YourControlHere' does not have a public property named 'SomeHtmlElement'."

Next you goggled all possible permutations of "asp.net server control tutorial" and "child controls". Most of the solutions seemed to be either, "call EnsureChildControls", or "Use a template". However, you already called "EnsureChildControls", and if the ASP Panel control doesn't use a template, why would you?

To fix this inconvenience simply put the following attribute before your control class:
[ParseChildren(false)]

Now you are free to go about your business calling this.RenderChildren where ever and when ever you please!