Archivio per la categoria ‘Script’

ASPNET Tips 'n' TricksIt’s a best practice storing scripts like js, or css, in an external file, the only exception to this rule is for lightweight script specific for a page like the following example:

$(function() {
alert('do something');
});

in the other cases the external file is the best choice for scalability, maintainability, degradability of your code. Asp.Net Web Forms is not supports the special grapheme for including external file .js using tag <script></script>.

The Visual Studio compilator not signs this syntax like an error or like a warning:
<script type=”text/javascript” src=”~/Scripts/jquery-1.4.1.min.js”></script>

But running our appliaction on Cassini we have this run-time error: $ undefined.

Javascript ettor

How can we solve this problem?

We have many ways to solve the problem with advantages and disadvantages.

Solution nr. 1 : using absolute path

Using syntax: <script src=”/Scripts/jquery-1.4.1.min.js” type=”text/javascript”></script> the script is include correctly.

This are the advantages:

  1. Standard HTML code
  2. Easy solution

This is the disadvantage:

  1. If the root of your application is different when will deploy it, the problem will not to be resolve.

Solution nr. 2: resolve the script with ResolveUrl

ResolveUrl is a method (of namespace System.Web.UI), supported by .Net Framework 1.0,  for converting a URL into one that is usable on the requesting client. We can solve, therefore, our problem with follow syntax:

<script src='<%=  ResolveUrl(“~/Scripts/jquery-1.4.1.min.js”) %>’ type=”text/javascript”></script>

the advantages of this choice are:

  1. easy solution
  2. solution indipendent by root of application

the disadvantage is that the solution is not a standard and performances of the page decrease because we are using server function inside a client declaration.

Solution nr. 3 : using ASP-NET Ajax

ScriptManager is the first aspnet control inside a web page. For MSDN It manages ASP.NET Ajax script libraries and script files, partial-page rendering, and client proxy class generation for Web and application services. We can use it for all application that we have written with MS Framework 3.0+ based on Asp.Net Ajax technology.

Using ScriptManager control we can solve our problem with this markup:

<form runat="server">
<asp:ScriptManager ID="sm1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.4.1.min.js" />
</Scripts>
</asp:ScriptManager>
<script type="text/javascript">
$(function () {
alert('Do something');
});
</script>

//

It’s a best practice (only for internet applications)  add commons libraries, like jQuery core, from a web repository. With ASP.NET 4.0 we can do this with simple two steps.

1. Insert this code into the section Application_Start of Global.asax.cs

void Application_Start(object sender, EventArgs e)
{
// map a simple name to a path
ScriptManager.ScriptResourceMapping.AddDefinition("jQuery", new ScriptResourceDefinition
{
Path = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
DebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
});

}

2. Change the script manager mark up with this declaration:


<form runat="server">
<asp:ScriptManager ID="sm1" runat="server">
<Scripts>
<asp:ScriptReference Name="jQuery" />
</Scripts>
</asp:ScriptManager>
<script type="text/javascript">
$(function () {
alert('Do something');
});
</script>

the advantage of this solution is the easy implementation on asp.net ajax web application. The disadvantage is that the ScriptManager load many libraries and than the page will be heavier than simple HTML page.

The CDN solution not working if your application is an intranet asp net project with some with limitations in web surfing.

rien ne va plus and happy coding  =)

Annunci

Torniamo a scrivere sul nostro blog dopo lunghi impegni di lavoro, in questo periodo abbiamo affrontato e superato diverse problematiche e, da oggi e nei giorni a seguire, vorremmo mettere la nostra esperienza a disposizione dei nostri lettori. In questo articolo forniremo alcune nozioni sulla copia e sul download di file da spazio FTP tramite script per sistemi operativi Windows.

Entrare in uno spazio FTP da sistema operativo Windows è di una semplicità estrema, basta infatti digitare il comando FTP dal prompt dei comandi per vederci catapultati nel nostro spazio web come in figura:

Una foto che visualizza il propt dei comandi con l'accesso allo spazio FTP

Nella tabella (in inglese) possiamo vedere i comandi eseguibili nello spazio FTP sono i seguenti:

FTP commands
Command Description
!   escape to the shell
?   print local help information
append   append to a file
ascii   set ascii transfer type
bell   beep when command completed
binary   set binary transfer type
bye   terminate ftp session and exit
cd   change remote working directory
close   terminate ftp session
debug   toggle debugging mode
delete   delete remote file
dir   list contents of remote directory
disconnect   terminate ftp session
get   receive file
glob   toggle metacharacter expansion of local file names
hash   toggle printing `#’ for each buffer transferred
help   print local help information
lcd   change local working directory
literal   send arbitrary ftp command
ls   nlist contents of remote directory
mdelete   delete multiple files
mdir   list contents of multiple remote directories
mget   get multiple files
mkdir   make directory on the remote machine
mls   nlist contents of multiple remote directories
mput   send multiple files
open   connect to remote tftp
prompt   force interactive prompting on multiple commands
put   send one file
pwd   print working directory on remote machine
quit   terminate ftp session and exit
quote   send arbitrary ftp command
recv   receive file
remotehelp   get help from remote server
rename   rename file
rmdir   remove directory on the remote machine
send   send one file
status   show current status
trace   toggle packet tracing
type   set file transfer type
user   send new user information
verbose   toggle verbose mode

La cosa che più ci può essere utile in questa situazione, non è tanto però, l’utilizzo della shell (che sarebbe comunque richiamabile soltanto in locale), quanto la creazione di uno script che potrebbe automatizzarci il lavoro. Un esempio di script per la copia FTP potrebbe essere il seguente:

MyUserId
MyPassword
cd files/pictures
binary
prompt n
mget *.*

Salvate questo script con il nome script.ftp  (la cui funzionalità è quella di copiare tutti i file presenti nella cartella pictures) e, per utilizzarlo, basta lanciare da riga comando la seguente istruzione:
FTP -s:script.ftp ftp.myhost.net
sostituendo i valori di MyUser, MyPassword e MyHost.net con quelli che vi interessano avrete un facile script per l’invio/ricezione dei dati. Attenzione però, essendo le password in chiaro, riteniamo che sia best practice quella di creare “on the fly” il file script e, dopo averlo, eseguito rimuoverlo.