View Single Post
  #6 (permalink)  
Old February 17th, 2014, 06:40 PM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

Hi Alan,

Sorry for my late reply. I wanted to dig into this before replying, but I never found the time to do so.

I think you found an issue in the code. Using unobtrusive validation requires an additional library and some code. I remember that the behavior changed somewhat between the release candidate and RTM version of VS 2012 and it looks like I missed a step when reviewing the final manuscript. The issue went unnoticed because the ContactForm is wrapped in an UpdatePanel, hiding the fact that the user control does NOT validate at the client anymore, but only at the server (after which the results in the ValidationSummary are sent to the browser as HTML). By enabling the ShowMessageBox option this became apparent, as that option works only at the client, before the postback. Since the form didn't do client side validation, the message box never showed up.

Fortunately, the fix is easy. Here's what I have in my upcoming 4.5.1 book which should resolve the issue:

1. In order to use unobtrusive validation together with jQuery, you need to install the package Microsoft.AspNet.ScriptManager.WebForms. To do this, open the Package Manager Console, enter the following command and hit enter:
Install-Package Microsoft.AspNet.ScriptManager.WebForms
2. After a short delay you should get a confirmation that the package has been installed. In addition, you should see a number of files appear in the WebForms folder located under the Scripts folder.

3. Open up the master page and change the reference to the jQuery library so it points to the ScriptResourceDefinition you created in the Global.asax file and insert a reference to the file WebUIValidation.js which handles the client side validation.
<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Path="~/Scripts/WebForms/WebUIValidation.js" />
If you didn’t make this change, jQuery would be included twice on pages that use the validation controls; once from the code in Global.asax, and once from the reference in the ScriptManager control. By having the ScriptReference point to the ScriptResourceDefinition defined in
Global.asax, ASP.NET sees that it’s pointing to the same JavaScript library and includes the reference only once.

If I am not mistaken, the change to the jQuery library is also in the 4.5 book, so I don't think you need that step.

After that, the control should validate at the client, and ShowMessageBox should work as advertised.

Hope this helps (and works!).

Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote