Vous êtes sur la page 1sur 9

====================================================================== ------------------------------------------------------------------------------The information in this article applies to: - Microsoft Visual Basic.NET (2002) ------------------------------------------------------------------------------IN T I! T"!# - !

$MM"%& - %e'uirements - (e)u**in* +lient-!i,e !cript in Visual Basic .NET - +reate the "pplication - (e)u* the "pplication - Mo,if- the +o,e - +omplete +o,e .istin*s - +reate the "pplication - (e)u* the "pplication - Mo,if- the +o,e - %E/E%EN+E! !$MM"%& ======= This step-)--step article ,escri)es ho0 to ,e)u* client-si,e script in a Microsoft "!1.NET application )- usin* Visual Basic .NET an, the Microsoft !cript (e)u**er. Visual Basic .NET pro2i,es a num)er of ne0 ,e)u**in* features that ena)le -ou to more easil- i,entif- an, ,ia*nose pro)lems in -our co,e. %e'uirements -----------The follo0in* list outlines the recommen,e, har,0are3 soft0are3 net0or4 infrastructure3 an, ser2ice pac4s that are re'uire,: - Microsoft Visual !tu,io .NET - Microsoft Internet Information !er2ices (II!) 5.0 or later - Microsoft !cript (e)u**er This article assumes that -ou are familiar 0ith the follo0in* topics: - 6e) applications - "!1.NET - Visual Basic .NET (e)u**in* +lient !i,e-!cript in Visual Basic .NET ------------------------------------------------In earlier 2ersions of Microsoft "cti2e !er2er 1a*es ("!1)3 applications can )e har, to ,e)u*3 particularl- if the )u*s occur in client-si,e script co,e. Visual !tu,io .NET allo0s -ou *reater control 0hen -ou ,e)u* client-si,e script throu*h the inte*rate, ,e)u**er an, the .ocals 0in,o0. +reate the "pplication: In this section3 -ou create an "!1.NET 6e) application that ,ispla-s three TM. Te7tBo7 controls alon* 0ith a Button control that calculates the input. This sample uses 8a2a!cript to pro*ram the Button control. 9. To use the ,e)u**er 0ith client-si,e 8a2a!cript3 -ou must first ena)le script

,e)u**in* for the )ro0ser. To ,o this3 follo0 these steps: a. :pen Microsoft Internet E7plorer. ). :n the Tools menu3 clic4 Internet :ptions. c. :n the ",2ance, ta)3 locate the Bro0sin* section3 clear the ;(isa)le script ,e)u**in*; chec4 )o73 an, then clic4 :#. ,. +lose Internet E7plorer. 2. To create a ne0 "!1.NET 6e) application in Visual Basic .NET3 follo0 these steps: a. :pen Visual !tu,io .NET. ). :n the /ile menu3 point to Ne03 an, then clic4 1ro<ect. c. In the Ne0 1ro<ect ,ialo* )o73 clic4 Visual Basic 1ro<ects un,er 1ro<ect T-pes3 an, then clic4 "!1.NET 6e) "pplication un,er Templates. ,. In the Name te7t )o73 t-pe ;!cript(e)u**in*E7ample; (0ithout the 'uotation mar4s). =. !0itch to the TM. 2ie0 of the 6e)/orm9.asp7 file. >. +op- an, paste the follo0in* co,e insi,e the ?/:%M@ ta*s: ?ta)le@ ?tr@ ?t, 0i,th=;900;@ ?asp:la)el i,=;l)l/irstNum)er; runat=;ser2er; 6i,th=;925;@ /irst Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er9; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er9;@ ?At,@ ?Atr@ ?tr@ ?t, 0i,th=;900;@ ?asp:la)el i,=;l)l!econ,Num)er; runat=;ser2er; 6i,th=;925;@ !econ, Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er2; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er2;@ ?At,@ ?Atr@ ?tr@ ?t, 0i,th=;900;@ ?asp:.a)el i,=;l)l%esult; runat=;ser2er; 6i,th=;925;@ %esult: ?Aasp:.a)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7t%esult; t-pe=;te7t; siBe=;5; ma7len*th=;5; N"ME=;t7t%esult; rea,onl-@ ?At,@ ?Atr@ ?Ata)le@

?)r@ ?ta)le@ ?tr@ ?t, 0i,th=;950; ali*n=;center;@ ?input i,=;)tn(i2i,e; onclic4=;return )tn(i2i,eC+lic4e,(); t-pe=;su)mit; 2alue=; (i2i,e ; name=;)tn(i2i,e;@ ?At,@ ?Atr@ ?Ata)le@ This co,e creates the t0o input te7t )o7es3 a calculation )utton3 an, a thir, te7t )o7 that ,ispla-s the results. 5. +op- an, paste the follo0in* co,e into -our pa*eD ma4e sure that -ou position the co,e )loc4 )efore the first ?B:(&@ ta*: ?!+%I1T lan*ua*e=;<a2ascript;@ function )tn(i2i,eC+lic4e,() E 2ar intNum)er9 = 0D 2ar intNum)er2 = 0D 2ar int%esult = 0D intNum)er9 = ,ocument.all(Ft7tNum)er9F).2alueD intNum)er2 = ,ocument.all(Ft7tNum)er9F).2alueD int%esult = intNum)er9AintNum)er2D ,ocument.all(Ft7t%esultF).2alue = int%esultD return falseD G ?A!+%I1T@ This co,e pro*rams the )utton so that the follo0in* functions occur 0hen -ou clic4 the )utton: - %etrie2e input 2alues. - +alculate input. - (ispla- result. H. +lic4 !a2e. I. To test the pro<ect3 follo0 these steps: a. :n the (e)u* menu3 clic4 !tart to )uil, an, to run the application. 6e)/orm9 opens in the )ro0ser an, ,ispla-s t0o input te7t )o7es3 a result te7t )o73 an, a )utton. ). In the /irst Num)er te7t )o73 t-pe ;9H; (0ithout the 'uotation mar4s). c. In the !econ, Num)er te7t )o73 t-pe ;2; (0ithout the 'uotation mar4s). ,. +lic4 (i2i,e. Notice that %esult te7t )o7 ,oes not ,ispla- J as -ou mi*ht e7pect. e. +lose Internet E7plorer. (e)u* the "pplication: The Microsoft !cript (e)u**er is useful for ,e)u**in* client-si,e script in -our "!1.NET application. &ou can use the ;,e)u**er; 4e-0or, to in2o4e the Microsoft !cript (e)u**er pro*rammaticall- in the script co,e. 9. ",, the follo0in* co,e as the first co,e in the )tn(i2i,eC+lic4e, proce,ure: ,e)u**er This 4e-0or, in,icates 0here the !cript (e)u**er shoul, stop e7ecution an,

start the ,e)u**er. This 4e-0or, allo0s -ou to trace the 2alues of 2aria)les throu*hout the remain,er of the routine. 2. +lic4 !a2e. =. To run the pro<ect3 follo0 these steps: a. :n the (e)u* menu3 clic4 !tart to )uil, an, to run the application. ). 6hen the pa*e opens in the )ro0ser3 t-pe ;9H; (0ithout the 'uotation mar4s) in the /irst Num)er te7t )o7. c. In the !econ, Num)er te7t )o73 t-pe ;2; (0ithout the 'uotation mar4s). ,. +lic4 (i2i,e. Notice that e7ecution stops at the ,e)u**er 4e-0or,3 an, that control is transferre, to the Visual !tu,io .NET inte*rate, ,e2elopment en2ironment (I(E). e. +lose Internet E7plorer. >. %i*ht-clic4 the follo0in* line of co,e: int%esult = intNum)er9AintNum)er2D an, then clic4 Insert Brea4point. 5. :n the (e)u* menu3 clic4 +ontinue. H. :n the (e)u* menu3 point to 6in,o0s3 an, then clic4 .ocals. Notice that this sets the 2alue of intNum)er9 to 9H as e7pecte,. o0e2er3 notice that this also sets the 2alue of intNum)er2 to 9H3 0hich ,oes not correspon, to the 2alue that -ou t-pe,. The intNum)er2 2aria)le is assi*ne, the same 2alue as intNum)er9. I. :n the (e)u* menu3 clic4 !top (e)u**in* to close the ,e)u**er an, )ro0ser 0in,o0s. +ontrol is returne, to the I(E. Mo,if- the +o,e: 9. %eplace the co,e intNum)er2 = ,ocument.all(Ft7tNum)er9F).2alueD 0ith the follo0in* co,e to assi*n the correct 2alue to the intNum)er2 2aria)le: intNum)er2 = ,ocument.all(Ft7tNum)er2F).2alueD 2. +lic4 !a2e. =. To test the pro<ect a*ain3 follo0 these steps: a. :n the (e)u* menu3 clic4 !tart to )uil, an, to run the application. ). 6hen the pa*e opens in the )ro0ser3 t-pe ;9H; (0ithout the 'uotation mar4s) in the /irst Num)er te7t )o7. c. In the !econ, Num)er te7t )o73 t-pe ;2; (0ithout the 'uotation mar4s). ,. +lic4 (i2i,e. Notice that e7ecution stops at the )e*innin* of the )tn(i2i,eC+lic4e, routine. e. Ensure that the .ocals 0in,o0 is still open. :n the (e)u* menu3 clic4 !tep Into to step throu*h the co,e line )- line until -ou reach the %eturn statement. Note that the intNum)er9 an, the intNum)er2 2aria)les ,isplathe correct 2alues. f. :n the (e)u* menu3 clic4 +ontinue. +ontrol is returne, to the )ro0ser3 an, the %esult te7t )o7 ,ispla-s the correct result.

*. +lose Internet E7plorer. +omplete +o,e .istin* --------------------+reate the "pplication: ?KL 1a*e .an*ua*e=;2); "utoE2ent6ireup=;false; +o,e)ehin, = ;6e)/orm9.asp7.2); Inherits=;!cript(e)u**in*E7ample.6e)/orm9;K@ ?M(:+T&1E TM. 1$B.I+ ;-AA6=+AA(T( TM. >.0 TransitionalAAEN;@ ? TM.@ ? E"(@ ?title@6e)/orm9?Atitle@ ?meta content=;Microsoft Visual !tu,io.NET I.0; name=;NENE%"T:%;@ ?meta content=;Visual Basic I.0; name=;+:(EC."NN$"NE;@ ?meta content=;8a2a!cript; name=;2sC,efault+lient!cript;@ ?meta content=;http:AAschemas.microsoft.comAintellisenseAie5; name=;2sCtar*et!chema;@ ?!+%I1T lan*ua*e=;<a2ascript;@ function )tn(i2i,eC+lic4e,() E 2ar intNum)er9 = 0D 2ar intNum)er2 = 0D 2ar int%esult = 0D intNum)er9 = ,ocument.all(Ft7tNum)er9F).2alueD intNum)er2 = ,ocument.all(Ft7tNum)er9F).2alueD int%esult = intNum)er9AintNum)er2D ,ocument.all(Ft7t%esultF).2alue = int%esultD return falseD G ?A!+%I1T@ ?A E"(@ ?)o,- M!C1:!ITI:NINN=;Nri,.a-out;@ ?form i,=;/orm9; metho,=;post;@ ?ta)le@ ?tr@ ?t, 0i,th=;900;@ ?asp:la)el i,=;l)l/irstNum)er; runat=;ser2er; 6i,th=;925;@ /irst Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er9; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er9;@ ?At,@ ?Atr@ ?tr@ ?t, 0i,th=;900;@ ?asp:la)el i,=;l)l!econ,Num)er; runat=;ser2er; ?B%A@ 6i,th=;925;@ !econ, Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er2; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er2;@ ?At,@ ?Atr@

?t, 0i,th=;900;@ ?asp:.a)el i,=;l)l%esult; runat=;ser2er; 6i,th=;925;@ %esult: ?Aasp:.a)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7t%esult; t-pe=;te7t; siBe=;5; ma7len*th=;5; N"ME=;t7t%esult; rea,onl-@ ?At,@ ?Atr@ ?Ata)le@ ?ta)le@ ?tr@ ?t, 0i,th=;I5; ali*n=;center;@ ?input i,=;)tn(i2i,e; 2alue=; (i2i,e ; onclic4=;return )tn(i2i,eC+lic4e,(); t-pe=;su)mit; siBe=;50; name=;)tn(i2i,e;@ ?At,@ ?Atr@ ?Ata)le@ ?Aform@ ?A)o,-@ ?A TM.@ (e)u* the "pplication: ?KL 1a*e .an*ua*e=;2); "utoE2ent6ireup=;false; +o,e)ehin, = ;6e)/orm9.asp7.2); Inherits=;!cript(e)u**in*E7ample.6e)/orm9;K@ ?M(:+T&1E TM. 1$B.I+ ;-AA6=+AA(T( TM. >.0 TransitionalAAEN;@ ? TM.@ ? E"(@ ?title@6e)/orm9?Atitle@ ?meta content=;Microsoft Visual !tu,io.NET I.0; name=;NENE%"T:%;@ ?meta content=;Visual Basic I.0; name=;+:(EC."NN$"NE;@ ?meta content=;8a2a!cript; name=;2sC,efault+lient!cript;@ ?meta content=;http:AAschemas.microsoft.comAintellisenseAie5; name=;2sCtar*et!chema;@ ?!+%I1T lan*ua*e=;<a2ascript;@ function )tn(i2i,eC+lic4e,() E ,e)u**er 2ar intNum)er9 = 0D 2ar intNum)er2 = 0D 2ar int%esult = 0D intNum)er9 = ,ocument.all(Ft7tNum)er9F).2alueD intNum)er2 = ,ocument.all(Ft7tNum)er9F).2alueD int%esult = intNum)er9AintNum)er2D ,ocument.all(Ft7t%esultF).2alue = int%esultD return falseD G ?A!+%I1T@ ?A E"(@ ?)o,- M!C1:!ITI:NINN=;Nri,.a-out;@ ?form i,=;/orm9; metho,=;post;@ ?ta)le@ ?tr@

?tr@

?t, 0i,th=;900;@ ?asp:la)el i,=;l)l/irstNum)er; runat=;ser2er; 6i,th=;925;@ /irst Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er9; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er9;@ ?At,@ ?Atr@ ?tr@ ?t, 0i,th=;900;@ ?asp:la)el i,=;l)l!econ,Num)er; runat=;ser2er; 6i,th=;925;@ !econ, Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er2; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er2;@ ?At,@ ?Atr@ ?tr@ ?t, 0i,th=;900;@ ?asp:.a)el i,=;l)l%esult; runat=;ser2er; 6i,th=;925;@ %esult: ?Aasp:.a)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7t%esult; t-pe=;te7t; siBe=;5; ma7len*th=;5; N"ME=;t7t%esult; rea,onl-@ ?At,@ ?Atr@ ?Ata)le@ ?)r@ ?ta)le@ ?tr@ ?t, 0i,th=;I5; ali*n=;center;@ ?input i,=;)tn(i2i,e; 2alue=; (i2i,e ; onclic4=;return )tn(i2i,eC+lic4e,(); t-pe=;su)mit; siBe=;50; name=;)tn(i2i,e;@ ?At,@ ?Atr@ ?Ata)le@ ?Aform@ ?A)o,-@ ?A TM.@ Mo,if- the +o,e: ?KL 1a*e .an*ua*e=;2); "utoE2ent6ireup=;false; +o,e)ehin, = ;6e)/orm9.asp7.2); Inherits=;!cript(e)u**in*E7ample.6e)/orm9;K@ ?M(:+T&1E TM. 1$B.I+ ;-AA6=+AA(T( TM. >.0 TransitionalAAEN;@ ? TM.@ ? E"(@ ?title@6e)/orm9?Atitle@ ?meta content=;Microsoft Visual !tu,io.NET I.0; name=;NENE%"T:%;@

?meta content=;Visual Basic I.0; name=;+:(EC."NN$"NE;@ ?meta content=;8a2a!cript; name=;2sC,efault+lient!cript;@ ?meta content=;http:AAschemas.microsoft.comAintellisenseAie5; name=;2sCtar*et!chema;@ ?!+%I1T lan*ua*e=;<a2ascript;@ function )tn(i2i,eC+lic4e,() E ,e)u**er 2ar intNum)er9 = 0D 2ar intNum)er2 = 0D 2ar int%esult = 0D intNum)er9 = ,ocument.all(Ft7tNum)er9F).2alueD intNum)er2 = ,ocument.all(Ft7tNum)er2F).2alueD int%esult = intNum)er9AintNum)er2D ,ocument.all(Ft7t%esultF).2alue = int%esultD return falseD

G ?A!+%I1T@ ?A E"(@ ?)o,- M!C1:!ITI:NINN=;Nri,.a-out;@ ?form i,=;/orm9; metho,=;post;@ ?ta)le@ ?tr@ ?t, 0i,th=;900;@ ?asp:la)el i,=;l)l/irstNum)er; runat=;ser2er; 6i,th=;925;@ /irst Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er9; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er9;@ ?At,@ ?Atr@ ?tr@ ?t, 0i,th=;900;@ ?asp:la)el i,=;l)l!econ,Num)er; runat=;ser2er; 6i,th=;925;@ !econ, Num)er: ?Aasp:la)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7tNum)er2; t-pe=;te7t; ma7.en*th=;=; siBe=;5; name=;t7tNum)er2;@ ?At,@ ?Atr@ ?tr@ ?t, 0i,th=;900;@ ?asp:.a)el i,=;l)l%esult; runat=;ser2er; 6i,th=;925;@ %esult: ?Aasp:.a)el@ ?At,@ ?t, 0i,th=;50;@ ?input i,=;t7t%esult; t-pe=;te7t; siBe=;5; ma7len*th=;5; N"ME=;t7t%esult; rea,onl-@ ?At,@

?Atr@ ?Ata)le@ ?)r@ ?ta)le@ ?tr@ ?t, 0i,th=;I5; ali*n=;center;@ ?input i,=;)tn(i2i,e; 2alue=; (i2i,e ; onclic4=;return )tn(i2i,eC+lic4e,(); t-pe=;su)mit; siBe=;50; name=;)tn(i2i,e;@ ?At,@ ?Atr@ ?Ata)le@ ?Aform@ ?A)o,-@ ?A TM.@