Jquery and multiple Ajax

I have had 2 occasions where I need to know when the ajax call have been finished:
  1. When in the beginning of page I have to load specific items that the user have been seen last time
  2. When the page was full with ajax call  – and there was also a “submit” button that should send the information (saved already ) that the user have finished entering the fields .
So – jquery have 2 methods: ajaxStart and ajaxStop.
Code:
Put 2 divs on the page :
<div id=”divWait” style=”display:none”> Please wait…</div>
<div id=”divOK” style=”display:none”> All data submitted</div>
And put this on javascript:
$(“#divWait”).ajaxStart(function() {
$(“#divWait”).show();
$(“#divOK”).hide();
});
 $(“#divWait”).ajaxStop(function() {
$(“#divWait”).hide();
$(“#divOK”).show();
});

Javascript hell

In 1997-200x days, when I have been programming in VB3-6 , there was a problem named ”DLL Hell”.

I have been hoping that, with the introduction of .NET, we will escaping this – and I have had my request answered( ok, from .NET 2 )

However, a new problem is for WebDevelopers : JavaScript Hell. Why I am telling that ? Simple :

  1. I have an application that works well in IE 8Star – and uses Json.Stringify to post data. Normally, when I was trying with IE7, it does not work – because JSON.Stringify was not allowed …Of course , solution was adding javascript from http://www.json.org/js.html .
  2. Same application use jquery 1.4 + http://knockoutjs.com/ 1.1.2 (to can modify dynamically some textboxes) + other js( a date time formatter,jquery ui and others)- . Now , in IE9 RC , jquery1.4 does  not function – and you have to use jquery1.5.1.rc also.Please tell me how to know what modifications the other js files must have( in particular knockout) .

And the mock idea is not solving the issue. You must test either manually , either with an automated test for browser(like Selenium) and testing explicitely in browser version! And I have  speak only about IE, not Firefox and /or Chrome…

If you are a developer, how many javascript do you use? If more than 2 ,welcome to javascript hell!

Star : Please read the requirements from http://msdn.microsoft.com/en-us/library/cc836459%28v=vs.85%29.aspx :

Note:

Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.

Internet Explorer 8 opts into the JScript 5.8 language features when the document mode for Internet Explorer 8 is “Internet Explorer 8 Standards” mode. For other document modes, Internet Explorer uses the version 5.7 feature set.

JScript 5.8 includes native JavaScript Object Notation (JSON) support and the accessor methods for Document Object Model (DOM) prototypes.

Asp.NET MVC, Jquery and Razor – Cascading dropdown, retrieving partial views, Json send objects, handling errors

There are many blogs and other posts on using jQuery with MVC. You can find individual posts on:

·         How to send objects to an action method via JavaScript.

·         How to retrieve partial views.

·         How to handle errors.

asp.net mvc jquery razor cascading demo

I have written a sample showing how to do all of these together. The sample is written in  MVC 3 using Razor views

The sample demonstrates:

  1. Cascading dropdown ( see the button Populate from action and Cascading drop down)
  2. Populating a table from a Partial View(see the button Add New Employee(jquery call action and render)
  3. How to handle  error from action ( press Save 2 )
  4. How to send id parameters (long) from javascript to action ( press Delete 1 or Delete 2)
  5. How to send objects(Employee) from javascript to action ( press Save 1 or Save 2)

Without further ado, this is the project . Enjoy!

Jquery MVC Razor demo full

PS: If you seems that the code is too hard, here is a more detailed explanation :http://msprogrammer.serviciipeweb.ro/2011/12/05/jquery-ajax-request-and-mvcdetailed/

Programmer Sql Server Management settings

Please left click on “Tools (menu item)” in “Microsoft SQL Server Management Studio”
clip_image001[3]
Please left click on “Tools (menu item)” in “Microsoft SQL Server Management Studio”
clip_image002[3]
Please left click on “Options… (menu item)” in “&Tools”
clip_image003[3]
Please left click on “Prevent saving changes that require table re-creation (check box)” in “Options”
clip_image004[3]
Please left click on “Environment (outline item)” in “Options”
clip_image005[3]
Please left click on “At startup: (combo box)” in “Options”
clip_image006[3]
Please left click on “Open Object Explorer and new query (list item)”
clip_image007[3]
Please left click on “Text Editor (outline item)” in “Options”
clip_image008[3]
Please left click on “Transact-SQL (outline item)” in “Options”
clip_image009[3]
Please left click on “Line numbers (check box)” in “Options”
clip_image010[3]
Please left click on “Query Execution (outline item)” in “Options”
clip_image011[3]
Please left click on “Query Results (outline item)” in “Options”
clip_image012[3]
Please left click on “SQL Server (outline item)” in “Options”
clip_image013[3]
Please left click on “SQL Server (outline item)” in “Options”
clip_image014[3]
Please left click on “Results to Grid (outline item)” in “Options”
clip_image015[3]
Please left click on “Include column headers when copying or saving the results (check box)” in “Options”
clip_image016[3]
Please left click on “Multiserver Results (outline item)” in “Options”
clip_image017[3]
Please left click on “Add server name to the results (row)” in “Options”
clip_image018[3]
Please left click on “Page down (push button)” in “Options”
clip_image019[3]
Please left click on “OK (push button)” in “Options”
clip_image020[3]

MVC Zip Result

Sometimes you need to send to the user more than 1 file – or, maybe, the file is too large
The simplest way is : made a zip file that contains the others.

What do you need
1. SharpzipLib from http://www.icsharpcode.net/opensource/sharpziplib/ ( or download via NuGet in VS)
2. obtain the file(s) that you want as a string or as a byte[] – let’s say you have a byte[] to store in a str variable
3. make in your action something like that:

 var fcr = new ZipResult("Export.xls", str);
            fcr.AddFile("readme.txt","this zip file contains ..");
            return fcr;

4. copy the following code in your asp.net mvc projects:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Text;

namespace utilsResult
{
    public class ZipResult : FileResult
    {

        
        private Dictionary<string, byte[]> content = new Dictionary<string, byte[]>();
        public string FileNameZip;
        public ZipResult(string FileName, byte[] Contents)
            : base("application/octet-stream")
        {
            this.FileDownloadName = Path.GetFileNameWithoutExtension(FileName) + ".zip";
            AddFile(FileName, Contents);
        }
        public void AddFile(string FileName,  byte[] Contents)
        {
            content.Add(FileName, Contents);
        }
        public void AddFile(string FileName,string Contents, Encoding e = null)
        {
            if (e == null)
                e = ASCIIEncoding.ASCII;

            content.Add(FileName, e.GetBytes(Contents));
        }

        protected override void WriteFile(HttpResponseBase response)
        {

            using (ZipOutputStream zos = new ZipOutputStream(response.OutputStream))
            {
                zos.SetLevel(3);
                zos.UseZip64=UseZip64.Off;

                foreach (var item in content)
                {
                    ZipEntry ze = new ZipEntry(item.Key);
                    ze.DateTime = DateTime.Now;                    
                    zos.PutNextEntry(ze);
                    int count=item.Value.Length;
                    zos.Write(item.Value, 0, count);
                   
                    
                }                
            }
        }
    }
}

5. future improvements:
Zip the file(s) in a dll project to made fully testable!

ASP.NET MVC make users–roles fast

please left click on “Microsoft SQL Server Management Studio (push button)”
clip_image001
please create a database in “Microsoft SQL Server Management Studio”
clip_image002
please left click on “C:\Windows\system32\cmd.exe (push button)” and enter the following command : aspnet_regsql.exe -E -S .\SQLExpress -d proprii -A rm
clip_image003
please left click on “ListPanel (list)” in “Microsoft SQL Server Management Studio”
clip_image004
please left click on “Tables (outline item)” in “Microsoft SQL Server Management Studio”
clip_image005
please keyboard input in “Microsoft SQL Server Management Studio” [F5]
clip_image006
please right click on “aspnet_Applications (list item)” in “Microsoft SQL Server Management Studio”
clip_image007
please left click on “Select Top 1000 Rows (menu item)”
clip_image008
please left click in “Microsoft SQL Server Management Studio”
clip_image009
please left click on “aspnet_Users (list item)” in “Microsoft SQL Server Management Studio”
clip_image010
please right click on “aspnet_Users (list item)” in “Microsoft SQL Server Management Studio”
clip_image011
please left click on “Select Top 1000 Rows (menu item)”
clip_image012
please left click on “RdcDAL – Microsoft Visual Web Developer 2010 Express (Administrator) (push button)”
clip_image013
please edit web.config in your MVC application and change the connection string to point to your database
clip_image014
please scroll down until membership and change application name from / to /applicationName
clip_image015
please run your application and left click on “Log On (editable text)” in “Home Page – Windows Internet Explorer”
clip_image016
please left click on “User name (editable text)” in “Log On – Windows Internet Explorer”
clip_image017
please left click on “Register (editable text)” in “Log On – Windows Internet Explorer”
clip_image018
please left click on “User name (editable text)” in “Register – Windows Internet Explorer”
clip_image019
please input your details (I have put mine : ignatandrei)
clip_image020
please see that I am registered to the site
clip_image021
please verify to the database – left click on “Microsoft SQL Server Management Studio (push button)”
clip_image022
please left click in “Microsoft SQL Server Management Studio”
clip_image023
please press [F5] in “Microsoft SQL Server Management Studio”
clip_image024
please see the results – application name
clip_image025
please left click on “Execute (push button)” in “Microsoft SQL Server Management Studio” for the query that lists users.
clip_image026

Programmer retrospective – 2010

What have I done in 2010 ?
1. I started this blog in English( I have one in Romanian, too)

2. I have made a book (outdated) about ASP.NET MVC 2 tips and tricks , published on Amazon .
3. I have initiated a book about programming in VS2010 – alas, it’s in Romanian at http://ronua.ro/CS/media/p/216886.aspx

4. I have learned ASP.NET MVC by answering to the questions at 1146 and I have nominated moderator on ASP.NET forums.

5. I have made a project for Azure – it is call “backup” – a project that you can deploy on Azure and have abackup for your files – like Mozy or DropBox . It is here :http://myazurebackup.codeplex.com/
(and those are other mine projects on codeplex:

AjaxForWinForms
Change Data Capture Helper
CHM2Word
Cropper.TimeCapture
IEnumerable – DataTable Exp…
PrintScreen
SqlConsolidate
UsingLib
vhd to boot entry
windows azure backup

It was a good year for me!

Hope it was for you too!