Is MVC 2 client-side validation broken in Visual Studio 2010 RC? - asp.net

I can't seem to get client side validation working with the version of MVC released with Visual Studio 2010 RC.
I've tried it with two projects -- one upgrade from 1.0, and one using the template that came with VS.
I'd think the template version would work, but it doesn't. Added the following scripts:
<script type="text/javascript"
src="<%= Url.Content("~/Scripts/MicrosoftMvcValidation.js") %>">
</script>
<script type="text/javascript"
src="<%= Url.Content("~/Scripts/jquery.validate.js")%>">
</script>
which are downloaded to the client correctly. Added the following to my form page:
<% Html.EnableClientValidation(); %>
<%--yes, am aware of the EndForm() bug! --%>
<% using (Html.BeginForm()) { %>
<%--snip --%>
and I can see the client validation scripts have been added to the bottom of the form. But still client validation never happens.
What is worse is that in my upgraded project, the client validation scripts are never output in the page!
PLEASE NOTE: I am specifically asking about the version of MVC2 that came with VS2010 RC. Also, I do know how to google; please don't waste anybody's time searching and answering if you aren't familiar with this issue in the release candidate of Visual Studio. Thanks.
Error ID10T: User editing one page and testing another.
However, I can't get client-side validation working on the MVC2 template project. If anybody has an idea how to, say, get it working for the registration page, please answer.

I had the same problem also,
the examples by MS(scottGu and haacked) are a bit confusing since you don't really know on which version they are talking about, and haacked updates his post every time a new version is out so you it's not relevant to you :(.
To make jQuery validation work On VS 2010 do the following:
as you answered you need the MicrosoftMvcJQueryValidation.js file, several ways to get this file, i got it from the example project from haacked post: ASP.NET MVC 2 Custom Validation
remove all JS references and leave just this one and the jquery.validate.min.js
call <% Html.EnableClientValidation(); %> before the opening tag of the form
Dont forget to put some Html.ValidationMessageFor(.. on your fields since this is the trigger for adding the enteries to the client validation JSON object
i guess you did all the above, but left the references to MicrosoftMvcValidation.js on the page, notice at the end of this file there is the hook to the MS client side validation.
funny i just wanted to blog about this issue this morning and found your question,\hope this helps

I have had no luck getting this to work in MVC 2 RC. According to other questions here on SO, you have to get the MicrosoftMvcJQueryValidation.js file from the MVC Futures release, hold your left foot behind your head, and whistle Dixie for half an hour. I did this and more and have not been able to make it work.
I have, however, gotten it working by using the jQuery Validation plugin directly. More work, but it gets the job done.
Hopefully it will be fixed in RTM.

Related

Optimizing and organizing ASP.NET content

I have a new ASP.NET Web Forms project that was started about 6 months ago. Being as how the project is still fairly new and in its infancy, this hasn't been much of a problem till recently.
The application is an ASP.NET Web Form application with a C# and SQL Server back end and an HTML5 and jQuery front end. The application is mostly ajax powered with very little post backs. It consists of mostly jQuery ajax calls to C# WebMethods.
The problem I am running into is some of my aspx pages are over 50kb in size with all the javascript and jQuery that is in the aspx page. I would like to move a lot of this javascript and jQuery code into separate files, but most of it consists of code that use the aspx <%= %> tags, which makes moving it a difficult (if not impossible) task.
It is getting tedious to find things in the code. I am wondering if there is a way to optimize the code (by somehow reducing the size of the aspx file) and if there is a good way to keep all this jQuery and javascript organized better so that finding things are a little bit easier? Also, is there maybe a javascript minifier that will minify the javascript inside an aspx page?
Any help would be appreciated!
Since you are using AJAX, I'm assuming you're using HTML markup, not any of the web controls. If so, one thing you can do is trun of ViewState in the aspx page. I was in the same position a few years ago, and decided to switch the Asp.net MVC. I would recommend you look into that. It gives you a much better development model, with minimal overhead.
Also, is you JS/CSS minified? I'm still confused about why "...but most of it consists of code that use the aspx <%= %> tags, which makes moving it a difficult (if not impossible) task."
WebForms, like MVC, also now supports bundling and minifications of JS files, check this out: http://blogs.msdn.com/b/rickandy/archive/2012/08/14/adding-bundling-and-minification-to-web-forms.aspx
Update: As OP pointed out, for .Net 3.5, you can use http://getcassette.net/ for bundling and minification.
to avoid using <% %> in your js to be able to reference your controls you can set the ClientIDMode to "static", that way the ID you set for the controls is the one that will be rendered and in js you can reference the control with something like
asp.net
<asp:DropDownList ID="ddlStateName" runat="server" ClientIDMode="static"></DropdownList>
jquery
$("#ddlStateName").val();

If you can't add ASP includes in SharepPoint 2010... Then what?

I'm having an extremely difficult time trying figure how to implemented some type of way to use ASP includes in SharePoint 2010. If you can't please please please help me with another way of doing so.
I found a solution, but I'm not completely happy with..
============HTML============
<div class="js-include" title="test.html"></div>
============JQUERY============
$(function () {
//Trigger global pages
$(".js-include").each(function(){
var inc=$(this);
$.get(inc.attr("title"), function(data){
inc.replaceWith(data);
});
});});
See The Case of Server Side Includes in Sharepoint Continued for an example of how to use the XML Web Part to duplicate in SharePoint what the <!-- #include file --> directive accomplishes in ASP.
However, as #PaulLeigh said, since SharePoint is built upon ASP.NET, I would highly recommend embracing Master Pages and user controls as replacements for ASP styled server side includes.

Telerik Controls - What is the point of re-registering static scripts in an update panel postback?

I'm attempting to speed up my website by combining and minifying my [web|script]resource.axd files.
Something based off of and similar to this.
http://www.koders.com/csharp/fid2061F9773188F0AB36F0DC42BC6073E3A935F71F.aspx?s=cdef%3Ajquery (modified to work with ie6)
The telerik controls in my pages render a lot of resouce.axd script tags. They also appear in update panel ajax postbacks in the pipes format.
Left by themselves the script files are run once. However after I combine the files in the regular page post and an update panel update fires. The code gets run a 2nd time due to the script reference in the update panel ajax response.
This results in a bunch of Telerik "Namespace is already registered." errors.
(I think this is because it is a seperate script file with a different name however I still get an error when I have the same file name, Telerik.Web.UI.Orientation is already registered)
I've been thinking that I can just remove the script references from the update panel postback altogether as the scripts are already on the page and there should never be any new controls added to the page requiring new scripts.
I've implemented this and I don't see any issues so far. Could there be something forboding that I am forgetting about?
Or is this a valid assumption?
Examples:
Normal Telerik Post
<html>
...
<script src="webresource.axd?d=asdfasdfasdfasdfasdfasdfasdf1" />
<script src="webresource.axd?d=asdfasdfasdfasdfasdfasdfasdf2" />
<script src="scriptresource.axd?d=asdfasdfasdfasdfasdfasdfasdf3" />
<script src="scriptresource.axd?d=asdfasdfasdfasdfasdfasdfasdf4" />
...
</html>
Normal Telerik UpdatePanel Response, no issues when requested from non combined page, throws <namespace> is already registered error when requested from page with combined scripts.
1|...|...|...|
123|scriptBlock|ScriptPath|/ScriptResource.axd?d=asdfasdfasdfasdfasdfasdfasdf3|
456|scriptBlock|ScriptPath|/WebResource.axd?d=asdfasdfasdfasdfasdfasdfasdf2|
Combined Scripts
<html>
...
<script src="js.axd?path=gdfg78sdfgsd70fghsrg89dg0sdfh0sfh9sfgh" />
</html>
What you said makes sense and as long as its working I don't foresee any issues. But I haven't run into this issue myself and I use a lot of telerik controls in update panels. It does have me a little curious. I assume you are, but wanted to double check, that you're using the RadScriptManager, which will automatically combine those script files for you. And since you mentioned you're aiming to improve performance as much as possible, I'd also suggest looking at their StyleSheetManager and Compression.
http://www.telerik.com/products/aspnet-ajax/stylesheetmanager.aspx
http://www.telerik.com/products/aspnet-ajax/compression.aspx
http://www.telerik.com/products/aspnet-ajax/scriptmanager.aspx
Also, have you considered using their RadAjaxManager instead of UpdatePanels? It should be more lightweight.

Visual Studio confused by server code inside javascript

I ran into an annoying problem: the following code gives a warning in Visual Studio.
<script type="text/javascript">
var x = <%: ViewData["param"] %>;
</script>
The warning is "Expected expression". Visual Studio gets confused, and all the javascript code after that is giving tons of warnings. Granted, it's all warnings, and it works perfectly fine in runtime - but it is very easy to miss real warnings among dozen of false positives.
It was working the same way in VS2008, and it wasn't fixed in VS2010. Does anybody know if there is a workaround, or a patch?
You need to wrap the server side expression in quotes.
<script type="text/javascript">
var x = "<%: ViewData["param"] %>";
</script>
This is what I got from Microsoft:
Unfortunately, this is due to a design limitation of our editor. We have had multiple users provide the same feedback, but we do not have a good solution at this time, other than to avoid server-side generation of client script. We will consider this issue in our planning for the next Visual Studio release.
You can trick the IDE using quotes or line comments
original code:
<script type="Text/javascript">
<%If Page.IsPostback Then%>
alert("my javascript code");
<%End If%>
</script>
solution:
<script type="Text/javascript">
//<%If Page.IsPostback Then%>
alert("my javascript code");
//<%End If%>
</script>
The approach you're taking is not a particularly sturdy one. Obviously it wouldn't work if you wanted to move your Javascript code to a separate file in order to improve your page load times....
You're better off using hidden form fields to move data from the server to client script. Alternatively, you can build the variable setter JS code programmatically by doing a bunch of string concatenation, then using the ClientScriptManager.RegisterClientScriptBlock() method to inject it into the output.
I believe that you ask from visual studio to understand too difficult thinks.
How visual studio can know what is inside the string that you pass ?, its parameters, its more code, what it is ???.... How VS can know what it is so can tread them that way ?
So they decide that everything on script tag there must be JavaScript.
My opinion is that if you won to avoid this error, Write render this JavaScript on code behind and not inside aspx page.
string cPlaceMeOnScript = "<script type=\"text/javascript\">var x =" + ViewData["param"] +";</script>"
and use any method to place this string on the start of your script.
I can't believe how many responses say "Put the JavaScript in the code-behind instead". This is a very bad idea, and muddles your code by mixing presentation with business logic. As the original poster stated, this is only a mere warning. While annoying, it's not worth mixing your presenatational behavior in with your server-side code (a.k.a. writing poor code) over.
This is not the standard behavior, and it is just happening randomly, and in such a situation just renaming the file and then building will help, (and it is possible that even without building it will be solved), and you can then revert back.
You can also completely disable warnings on Java Script under Tools -> Options -> Text Editor -> JScript -> Miscellaneous.
Of course in this case you will miss the Java Script warnings, but at least you will be able to concentrate on the server side warnings.

How to implement A simple AJAX call using asp.net page?

I'm trying to implement this specific example using asp.net page instead of asp page.
If you look at the example you can see that there are 2 parts for the page:
Mail asp page. This page have JS code that calls other asp file for AJAX use.
the other asp page which holds the JS code.
The responseText of the call is the client side code, so, when I write something like this:
<html><head><title>test</title><script language="javascript" runat="server"
type="text/javascript">function test(){Response.Write("This is a Test!");
</script><body onload="test()"></body></html>
the page ignores my server side code and returns this:
<html><head><title>test</title><body onload="test()"></body></html>
what should I need to do to make him process my JS code and return its output?
Thanks in advance,
Oz Radiano.
asp.net does not process javascript server side, so setting the script tag runat=server with language="javascript" will be mostly ignored.
I think if you change it to "JScript" it will work, however, this has nothing to do with ajax.
"runat = server" says, preprocess this on the server and don't send it to the client.
If the language is a processable one it will be evaluated as well.
Try implementing the example after watching some videos from http://www.asp.net/learn/ajax-videos/ and http://www.asp.net/learn/ajax/
Its very easy to implement AJAX in asp.net then ASP. I can clearly give you the correct source code. :) But you seem to try out new things. Let us know how it goes!
Thanks for your responses, they made me understand that I'm not sure what my problem is.
After failing in implementing this exact example, I've googleed "how to run asp code using ajax"
this result returned and made it very clear.
Thanks again.

Resources