posted on Monday, January 14, 2008 2:17 PM | Filed Under [ Visual Studio 2008 Asp.Net ]

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:

image

After clicking LinkButton1 using VS 2008:

image 

Comments Leave Yours...
davehunt00
7/9/2008 6:43 PM
# re: LinkButton Always Triggers Validation In Visual Studio 2008 Products

I ran into similar behavior and it was nice to find I wasn't alone (sorry about that!).

I had data entry pages with requiredFieldValidators that work great (i.e., allow the user to navigate away via nav buttons if not clicking Submit), but in VS2008 they fire every time.

So far the only solution I've found is setting EnableClientScript=false for the validator and putting whatever code would be fired by the Submit button within an "if (isvalid)" condition.

This, of course, totally invalidates (pardon the pun) the point of a client-side validation control.

davehunt00

Post Your Comment

Title
Required
Name
Required
Email
Optional
Url
Optional
Comment  
Please add 1 and 6 and type the answer here:

Who Is Rafe

rafe

I Am Rafe Kemmis

I am an audacious entrepreneur with a double bachelor of science in Computer Science and Mathematics. I specialize in Microsoft ASP.Net web application development as well as accredited information systems auditing.

Questions?

Always a thoughtful response. You may post your question on an article, or contact me directly.

Hire Me.

I provide custom solutions to complex problems. I can help your business no matter how large or small.

Contact me now.

Subscribe