Why is a LinkButton driving me up the wall?
Last week I ran into some odd validation control behavior when I dropped a LinkButton on to a web form that I was working with. Whenever the LinkButton would post back to the server, all of the validation controls would execute their validation routines and fail. This in turn prevented the main page life-cycle from continuing. This of course was not the behavior I needed. I wanted my post-back to bypass the page validation and have my LinkButton's command to trigger.
I then turned to setting the CausesValidation property on the LinkButton to false. This typically is the only setting necessary to keep a LinkButton from triggering a page validation. But not in this case. I clicked on the LinkButton and yet again all of the validation on the page lit up like a Christmas tree. And still my LinkButton command didn't execute.
At this point I wasn't yet questioning my sanity. I proceeded to set unique ValidationGroup names for the controls on my page. The validation controls and their associated input controls received ValidationGroup="Group1". My LinkButton received ValidationGroup="Group2". I was sure this would fix the problem. But it didn't.
I started to feel completely incompetent as an Asp.Net programmer. I must not know enough about how to use a damned LinkButton. So, I replaced my LinkButton with a regular Button. And wow, this magically fixed all my problems! But being the inquisitive person that I am, my quest to solve this riddled did not stop there.
What's the deal?
Long story short, after several days of diagnosing the problem, which played out like a comical episode of House, the problem appears to be a behavior related to using Visual Studio 2008. When I debug my form in Visual Studio 2005, all LinkButton and validation behavior goes as expected. When I debug the web form using either Visual Studio Team System 2008 or Visual Web Developer Express 2008, the page validation goes haywire. This oddity happens regardless of which framework I target.
So what did I learn here? I sure can't fully explain why VS 2008 leads me to different validation behavior than VS 2005. The only modicum of utility that I am walking away with from this short inquisition is that you shouldn't overly trust a piece of software, no matter how nice it appears to be.
Please tell me I'm not going insane.
I am really interested in seeing if others get the same results. Try this out for yourself and see if you get different results using Visual Studio 2005 and Visual Studio 2008:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Oh My Gravy!"
ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Should Validate Button" />
<br />
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="false" />
After clicking LinkButton1 using VS 2005:
After clicking LinkButton1 using VS 2008: