Friday, May 27, 2005

SmartNavigation: Friend of Foe?

When studying for my certification exams, one thing I read about that seemed cool is SmartNavigaiton. I must confess that I had not used this feature at all in the past. For those who do not know about SmartNavigation, it is a page-level asp.net property you can set that is supposed to fix a number of web page post back woes. This easily set property is used to prevent the page flicker, and the loss of state related to where the user was on the form before the post back. This sounded like a great thing to try on a web form where there is a post back event linked to a text box TextChanged event. In my case, I am calculating a total cost based on the amounts entered in a few text boxes. Without SmartNavigation, when the page posted back, the focus was back to the top of the form. This is an annoying behavior to my users who had to enter a few different values that went into the total calculation. Each time they changed an amount field, they were popped back up to the top of the form.

SmartNavigation does take care of this issue, although if a user is in a text box that causes post back, and they tab off the field, the post back happens and SmartNavigation puts the focus back into the textbox they just left. This is better than moving them back to the top of the form. However, once I added a few more user controls and a style to my page, I ran into problems. I did not get a page error. Instead, IE reported a fatal error and my web browser closed. This happened to me repeatedly.

I followed my usual course of action when I run into crazy, unexpected behavior. I went to Google. I found a blog post by Karsten Samaschke dedicated to SmartNavigation, and why not to use it. In my situation, my style sheet may be the culprit. For now, I think I will turn SmartNavigation off.

And I was so excited about using something I learned while studying for the cert tests…

Wednesday, May 25, 2005

ASP.Net Custom Validators

The other day I came across a tidbit of information that once stumbled upon, I realized I had known this long ago. I wish I had remembered it before I had to debug code for an hour, so I thought in the interest of prosperity, I would document this tidbit right now.

In our web solution, we make use of user controls rather extensively. My client had the need for one of my common controls to be validated, but only in one instance of its implementation. I thought, "Hey! This is the perfect time to use a custom validator." I dropped on my custom validator, set the ControlToValidate to my user control, added the OnServerValidate event and voila. Except for one problem...I got an error when I tried to open the page. Apparently, a user control is not allowed to be the target of a custom validator. So what to do? As has happened in the past, and as I am sure will happen again in the future, I thought of a work around :)

I decided to set the target control to a text box I had on the same form. Oops. This text box did not require an entry. My custom validator only fired when there was text in the textbox. It took me a couple of seconds to recognize my mistake, as I tried to figure out why my validator worked sporadically.

Long story short (too late???), if you want to use a custom validator, the validation event will only fire if the control you are validating has an entry.