Tài liệu Programming Microsoft SQL Server 2000 with Microsoft Visual Basic .Net - P8 docx

50 462 0
Tài liệu Programming Microsoft SQL Server 2000 with Microsoft Visual Basic .Net - P8 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

A typical ASP.NET applicat ion will be run in an environm ent in which m any browsers ar e connect ed t o a single Web server or a farm of Web servers. For sim plicit y, I discuss the case of t he single Web server, but t he sam e general principles apply t o a farm of Web servers. Th e asynch ronous nat ure of com m unicat ion between a Web server and browsers facilit ates the m aint enance of a large num ber of concurrent browser sessions with a single Web server. Each of t hese browsers can have a differ ent session st ate wit h t he Web server. I n ot her words, t he sam e page can show different cont ent s t o differ ent users depending on t he int eract ions of each user wit h the Web server. At t he same tim e, a dat abase server can pr ovide a consist ent set of inform at ion across m ult iple browser sessions. Anot her defining char act er ist ic of Web applicat ions is t hat a Web server can serve pages t o m any different kinds of browsers. Alt hough all brow ser t ypes render HTML code in pages served from a Web server, the rendering isn’t necessarily invariant fr om one browser t ype t o t he next . Som e brow sers handle client -side script ing in one language but not anot her. Different browser versions target different HTML versions ( 3.2 and 4.0 are two com m on HTML versions). Som e support absolut e posit ioning, but support isn’t universal for t his feat ure. For differences such as t hose not ed, it ’s not uncom m on for Web applicat ions to have to “sniff” t he brow ser t ype to det erm ine it s charact erist ics and then send a page that is rendered and behav es well on t hat browser. ASP.NET offers a couple of approaches t o handling Web application developm ent issues. First , ASP.NET can elicit HTML from Web serv ers in an at t em pt t o m inim ize t he im pact of different browser t ypes on t he way t hat a page appears in a browser. Second, ASP.NET has built - in capabilit ies for detecting browser t ypes Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. and t heir capabilit ies. Your applicat ions can tap t hese capabilit ies to dir ect browsers t o pages t hat are rendered well wit hin t hem. N ot e I t’s a big drain on limit ed Web application developm ent resources to det ect browsers and prepare m ultiple pages that are rendered well in different browsers. As a business em ploying talented Web developers, wouldn’t you rather those developers spend t heir tim e cr eating m ore useful content in different pages than creating m ultiple copies of a sm aller number of less useful pages so as to optim ize for different browser s? Because ASP.NET is free wit h the II S 5.0 Web server, and t he Internet Explorer browser is free with m ost Microsoft product s and from Microsoft ’s I nt ernet site, I recom mend you use the Internet Explorer browser for your ASP.NET applications. Note t hat Micr osoft didn’t encourage m e t o m ake this st atem ent. I nst ead, I m ak e the st atem ent on t he basis of m y observations about how taxing it is on organizations to cr eat e Web solut ions t hat work well with m ult iple browsers. Pages and t he Pa ge Cla ss As indicat ed in Chapt er 8, ASP.NET builds on ASP. For exam ple, Web servers can discern an ASP.NET page based on it s ext ension, .aspx. The extension for an ASP.NET page is the sam e as t he ext ension of ASP pages w it h an x appended t o it. For exam ple, an ASP.NET page will have a nam e like m ypage.aspx. Any ASP.NET solut ion can have m ult iple pages wit hin it . You can designate one page as t he st art page for an applicat ion. (There is no st art up obj ect as there is wit h Windows applicat ions. ) An ASP.NET solut ion can contain m ultiple pages, and users can navigate wit hin and bet ween t he pages of a solut ion. I n addit ion, you can navigat e t o old .asp page files or even ot her types of files, such as .ht m or .ht m l files. When an ASP.NET applicat ion opens an .aspx file, t he applicat ion launch es t he Page_Load event procedure ( unless you rem ove it) . The Page class is a program m ing abst ract ion of an .aspx file. This Page class provides m any valuable services. For exam ple, you can use a page’s I sPost Back property t o det erm ine whether a page is being displayed for t he first tim e in a session or is the result of a browser post ing a page back t o a Web server. A page’s Session propert y enables your application to m anage variables across m ult iple asy nchronous connect ions t o a Web server fr om a browser wit hin a session. A page’s Applicat ion propert y point s t o an Applicat ion object for sharing variable values acr oss an applicat ion’s users. One of the m ost excit ing feat ur es of t he Page class is t he way t hat it autom ates t he m anagem ent of form at t ing and cont ent bet w een round-t rips; I ’ll discuss t his t opic m ore fully in t he “Session St ate Managem ent ” sect ion. Wit h t he Response and Request properties for a page, you can explicit ly m anage the exchange of inform at ion bet ween a Web server and a brow ser. The Response property enables you t o echo cont ent t o Web pages so t hat you can m onit or t he ingoing and out going v alues for controls and variable values on Web pages. Developers w ill r ecognize t his capabilit y as particularly handy for debugging. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. My personal favorite feature of ASP.NET is t he way t hat it separat es page layout issues from pr ogram logic. Those of you fam iliar wit h ASP w ill recall how you had the opportunit y (indeed it was a r equirem ent) t o m ix HTML layout w it h your script for m anaging t he logic on a page. Alt hough you can st ill do t his, Visual St udio support s a t w o- file st ruct ur e for t he Web page class. The file with t he .aspx ext ension cont ains t he HTML layout code. A new, second, file cont ains the program logic. This file’s extension depends on the program m ing language. For exam ple, if your Web page file has t he nam e yourpage.aspx, it s code file in Visual Basic w ill hav e t he nam e yourpage.asp x.vb. The page w it h t he .asp x ext ension cont ains controls, such as labels, t ext boxes, and list boxes. The code file (w it h the aspx.vb extension) includes page init ializat ion code, event procedures for t he obj ect s on t he page, and other rout ines and declarat ions t o m anage t he behavior of a page. An ASP.NET applicat ion t hat y ou develop w it h Visual St udio has t w o folders for m anaging a solut ion. Your .aspx and .aspx.vb files reside in a folder on t he Web server. The proj ect folder ’s default locat ion is in t he wwwroot subdirect ory of the I net pub folder of t he I I S serv er host ing your applicat ion. You can have m ult iple files wit hin t he Web folder for a solut ion, including ot her Web pages and graphic files. You open a Web solut ion by invok ing any .aspx file wit hin t he folder on t he Web server . A second folder in your Visual St udio solut ion direct ory cont ains t he solut ion file ( .sln) for an ASP.NET pr oj ect . This second folder is the sam e one Visu al St udio uses t o st ore the files for your Window s applicat ion solut ions. Use the .sln file t o open your solut ion in Visual St udio when you w ant t o edit t he design of a solut ion. Bot h the Web folder and t he regular Windows folder can have the sam e nam e t o facilit ate t heir coordination. Cont rols on Pa ges You st art an ASP.NET proj ect in Visual Studio .NET by click ing New Pr oj ect on t he Visu al St udio .NET St art Page and t hen highlight ing t he ASP.NET Web Applicat ion tem plat e. Designat e a folder nam e for your new proj ect . Th e default locat ion is on the localhost I I S server. See t he over view of ASP.NET in Chapt er 8 for t he three elem ent s t hat you need t o creat e ASP.NET solut ions. As Visual St udio .NET st arts the proj ect , it creat es a Web sit e for the solut ion on t he Web serv er. When t he proj ect opens, y ou w ill view an em pt y Web page ( w it h t he .aspx ext ension) . The default nam e for t he page is WebForm 1.aspx. You can assign a new nam e t hat is m ore m eaningful in t erm s of your applicat ion by right-clicking the file’s nam e in Solut ion Explorer and choosing Renam e to assign a new nam e. Use t he Toolbox t o add cont rols t o a blank page. You can display t he Toolbox by choosing Toolbox from t he View m enu from wit hin Design v iew for t he st art up page. There are t hr ee cat egories of nat ive controls that you ar e likely t o add t o an .asp x page. These reside in t hree separat e t abs wit hin t he Toolbox. • First , you can click t he HTML tab in t he Toolbox t o expose classic HTML cont rols. These controls can operate as HTML elem ent s or as a new t ype of HTML server cont rol. I f you are used t o building Web solut ions w it h ASP or som e other Web developm ent language ( such as Perl) , you probably already have a work ing knowledge of HTML cont rols. One key dist inct ion bet w een classic HTML form cont rols and HTML server controls is t hat server cont rols offer an obj ect m odel for program m ing on a Web server. • Second, you can click the Web Form s t ab in t he Toolbox t o ex pose Web server form controls designed explicitly for use wit h ASP.NET. These cont rols are r endered as HTML on Web pages, but t hey have properties, m et hods, and event s associat ed w it h them . You can use Web server cont rols on t he ASP.NET Page class inst ances sim ilarly to t he w ay you use Windows form controls on form s in Windows applicat ions. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. • Third, y ou can use t he item s on t he Dat a t ab in t he Toolbox t o invoke wizards for declaring and inst ant iating ADO.NET obj ect s, such as a data adapt er for SQL Server or a dat a set. These ADO.NET obj ect s reside on a page at design t im e, and t hey can t hus sim plify your code by reducing the need t o declare and inst ant iate object s. See Chapt er 1 for the discussion of an exam ple t hat dem onst rat es t he use of t hese form s in a Windows applicat ion. This ch apt er cont ains supplem ent ary ASP.NET applicat ions that illust rat e t he use of Dat a cont rols on Web pages. HTML cont rols and Web serv er controls bot h represent t ext boxes, but t ons, and sim ilar kinds of cont rols. The list of HTML control it em s m aps t o t he classic HTML form cont r ols, such as a label, a t ext field, a subm it but t on, and a reset but t on. HTML cont rols can run eit her as classic HTML form cont rols or as HTML server cont rols. You need to designat e a special attribute (runat= "server" ) in order for the it em s on t he Toolbox’s HTML tab t o be available for pr ogram m ing on a Web server. HTML cont rols w it h the special at t ribut e set t ing are oft en designat ed HTML server cont rols in t he ASP.NET docu m ent at ion. You m ust give HTML ser ver cont rols a nam e at design tim e by assigning a value t o their I D at t ribute in order to reference t hem program m atically on a Web ser ver. Wit hout t he special runat at t ribut e set t ing, HTML controls pass t heir values along in eit her of t he t w o tradit ional ways: in t he HTTP header or in t he body of a form on an .aspx Web page. I f you use t he special attribut e set t ing, you can m anipulat e HTML cont rol propert ies sim ilarly t o t he way you m anage cont rols on a Window s form . N ot e The HTML form ’s m ethod at tribute t ells a Web server where to look for values wit h a form on a Web page. The “get ” sett ing for a m et hod att r ibute says t o look in the HTTP header. This is a query st ring appearing after the URL t o which a Web page navigates. The “post ” set ting designat es the storage of values in t he body of a Web page. Web server cont rols convey HTML to browsers. However, t he HTML syntax for these cont rols doesn’t m ap in a one- t o- one way to HTML form cont rols. For exam ple, t he RadioBut t onList Web server cont rol cont ains t ext inside an HTML table elem ent . This cont rol doesn’t m ap t o t he HTML Radio But t on control, alt hough bot h cont r ols can hav e a sim ilar look on a Web page. Select ed Web server cont rols include But t on, Label, List Box , Dat aGrid, HyperLink, and about 25 m ore. You can see t he full list of Web server cont r ols wit h links for dr illing down further int o t he properties, m et hods, and ev ent s for each one in t he “Web Server Controls” t opic of t he Visual St udio .NET docum ent at ion. N ot e You can open the Visual St udio .NET docum ent ation fr om the Program s m enu on the Window s St art butt on. Choose Microsoft Visual St udio .NET Docum entat ion from t he Microsoft Visual St udio .NET it em on t he Program s m enu. Use t he Search t ab to ret urn a list of item s m at ching a search crit erion, su ch as Web Server Controls. I f you have difficulty finding a particular topic because too m any it em s ret urn, sort by Tit le by click ing t he Tit le header in the Search results window. Then scroll through the item s, which are now Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. list ed in alphabetical order, to the it em you want. Web server cont rols and HTML server cont rols share select ed feat ur es in com m on. For exam ple, one killer feat ure is t he abilit y for form cont rols to m aint ain t heir values on round-trips to a Web server. This m aint enance of form field values can require a lot of program m ing w ith tradit ional HTML cont rols on HTML forms. However, Web server cont rols and HTML server cont rols provide t his feat ure w it hout any program m ing. Anot her significant feat ure shared by Web server cont rols and HTML server cont rols is t he abilit y t o use t he validat ion cont rols t o assess t he content of cont rols on a form . Validat ion cont r ols enable certain validit y t est s, su ch as whether t he cont r ol cont ains an ent ry, whet her t he cont rol has a value in a specified form at , and whet her t he value is in a specified range. Again, no program m ing is necessary for base funct ionalit y. How ever, lear ning a few program ming trick s for t he validat ion cont rols can enable you t o refine t he user experience. I st rongly urge y ou t o use Web ser ver controls inst ead of HTML server cont rols in all your ASP.NET applications. As t heir nam es im ply, Web server cont r ols run aut om at ically on t he Web server. There is no special at t ribut e for you to set in order t o use t he cont r ols on a Web server. Web serv er cont rols are m uch m ore ext ensive in their variet y than HTML server cont rols. I n addition t o t he st andard HTML cont rol t ypes, Web serv er cont rols offer a variet y of specialized cont rols to facilitat e y our display and m anipulat ion of data, including a Dat aGrid cont rol for displaying dat a as a table. Dev elopers who build solut ions for ent ering or showing dat es are likely to find t he Web server Calendar cont rol of special value. This cont rol enables entering and displaying dates on a st andard m ont hly calendar display. The general HTML form at for a Web server control appears her e. The asp prefix denotes t he cont r ol as a Web server cont rol. The cont rolnam e param et er specifies the t ype of cont rol (for exam ple, label) . The attribut es param eter corresponds to a list of property set t ings. <asp:controlname attributes runat="server"> ASP.N ET D esign I nt erface s I have been developing Web solut ions pr im arily with Micr osoft t echnology since approx im at ely 1995, and the ASP.NET design int erface is by far m y favorit e over that t im e span. This is because t he ASP.NET design interface offers you the flexibilit y of dev eloping a Web applicat ion very m uch as you cr eat e Windows solut ions wit h Visual Basic .NET. I t leaves m e fr ee t o focus on m y solution inst ead of being dist ract ed by Web layout dev elopm ent issues. I n ot her w ords, ASP.NET provides a design environm ent t hat m akes m e feel as t hough I ’m cr eat ing a typical Window s solut ion— except it ’s for the Web. (See Chapt er 8 for m or e coverage on t his point.) Nonet heless, the ASP.NET design env ir onm ent st ill has som e unique feat ur es, which will be covered in this sect ion. Aft er init ially cr eat ing a new ASP.NET proj ect ( as descr ibed in t he preceding sect ion) , you are confronted w it h t he HTML Designer, which displays a blank Web layout page. By default , this page will hav e t he filenam e WebFor m 1.aspx. The first point to not e is t hat t he t ab for t he page has two cont rols at it s bot t om . The page opens w it h the Design cont rol select ed (unless you ch ange t he default set t ing) , which pr esent s a graphical view of t he page. Click ing the HTML cont r ol displays t he HTML code behind t he page. I n ot her words, t he HTML Designer offers two views for each page— a Design view for graphical dev elopm ent of a page and an HTML code view for program m at ic design of a page. When y ou look at t he page in HTML view, you’ll see t hat it st arts w it h an @Page direct ive. I n t his direct ive are set t ing assignm ent s that you should t ypically not edit . Next t here is a docu m ent declarat ion declaring t he page to be an HTML 4.0 docum ent. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Aft er t he t w o prelim inary declarat ion st at em ent s, the HTML code w indow displays an ht m l t ag. A m at ching / ht m l t ag ends t he docum ent . Ot her HTML tags nest bet w een t hese beginning and ending t ags for the HTML cont ent w it hin a docum ent. Tw o set s of m at ching t ags nest direct ly beneat h t he ht m l and / ht m l tags. The cont ent bet ween the head and / head t ags is m ostly boilerplate m at erial rout inely prepared by Visual St udio for all .aspx page files t hat it generates. Howev er, t he t it le and / title t ags cont ain t he title t hat appears in t he t itle bar of a Web browser window w hen t he brow ser display s the page. You can edit a page’s title eit her from t he HTML code window or t he Propert ies window for t he docum ent. Changes in either locat ion updat e t he other locat ion autom at ically. The next pair of nest ed t ags wit hin t he ht m l and / ht m l t ags are t he body and / body t ags. Unless you change t he default sett ings, t he body t ag will have an MS_POSI TI ONI NG at t ribut e set t ing of GridLayout . This handy set t ing let s you drag and dr op cont rols around a Web page just as on a Windows form . The MS_POSI TI ONI NG at t ribut e set t ing corresponds to t he pageLayout pr operty set t ing in t he Propert ies window for t he docum ent . The default set t ing for t he pageLay out set t ing m at ches t he MS_ POSI TI ONI NG at t ribut e set t ing of GridLayout . Choosing the ot her property set t ing of FlowLayout for t he pageLayout property rem oves t he MS_POSI TI ONI NG at t ribute from t he HTML code window. This alternat iv e layout set t ing allows y ou to posit ion cont rols on a Web page as you do cont ent on a t ypical word processing docum ent. Th is is the t radit ional HTML way of posit ioning cont rols on a page, and it provides your great est com patibilit y with br owser t ypes— especially older browser t y pes t hat don’t support absolut e posit ioning. How ever, y ou give up t he convenience of being able to drag and drop cont rols on a Web page. The form and / form tags reside w it hin the body t ags. All the cont rols on a Web page appear wit hin t he form and / form tags. I will discuss these t ags next as I r eview t he page design for our WebApplicat ion1 proj ect , w hich was init ially discussed in Chapt er 8. N ot e The WebApplicat ion1 sam ple, like m ost of the sam ples in t his chapt er, exist s as t wo folders. Both folders hav e t he nam e WebApplication1. One contains just two files in its root . One of these files is WebApplication1.sln. The folder cont aining this file belongs in t he direct ory that your com puter uses for st oring Visual Basic .NET Windows solut ions. You can open the WebApplicat ion1 solut ion in Visual St udio by choosing Open Solution from the File m enu and select ing WebApplication1.sln. You m ay need to browse t o the folder containing WebApplication1.sln befor e being able to select it . The second folder contains WebFor m1.aspx in it s root . This folder belongs in t he wwwroot direct ory of the Inet pub folder on t he com puter from which you will be r unning t he applicat ion. You can open the Web page by browsing ht t p: / / < webserver> / WebApplicat ion1/ WebForm 1.aspx. Recall t hat t he WebApplicat ion1 proj ect from Chapter 8 cont ains t wo cont r ols: a button and a label. Click ing t he but t on causes t he label t o show “Hello World.” Figure 11-2 reveals t he but t on cont r ol above t he label cont rol in Design view for WebApplicat ion1’s WebFor m 1.aspx file. Bot h cont rols are Web server cont rols. I select ed t hem fr om t he Web For m s t ab of t he Toolbox. When y ou add a cont r ol from t he Toolbox t o a Web page, you can select it in the Toolbox and t hen size the cont rol by dragging it out on your Web page. Alt ernat iv ely, you can right -click the cont rol t ype in t he Toolbox and choose Copy. Then right - click in Design v iew Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. for the Web page, and choose Paste. This adds the control wit h pr econfigur ed size set t ings in t he upper left corner of t he page. I f your docum ent has a Gr idLayout set t ing for its pageLayout pr operty, you can t hen j ust drag t he cont rol to it s desired locat ion. Figure 11-2 shows t he label and but t on cont r ols w it h t heir preconfigured size sett ings. Figur e 1 1 - 2 . The ASP.N ET D e sign vie w for W e bFor m 1 .a spx in t he W e bApplicat ion 1 proj e ct . Figure 11-2 sh ows sev eral t oolbars. All t he cont rols on t he bot t om row, after t he first t w o, belong t o t he Layout t oolbar. I f t his t oolbar isn’t visible, y ou can m ake it appear in t he usual way for Windows applications— right - click any t oolbar, and select t he one y ou want (Layout ) from t he cont ext m enu. ( You can also select Toolbars from t he View m enu and select t he appropriat e toolbar from t he list .) The cont r ols on t his t oolbar can m ake fast work of t ypical form layout design task s, such as aligning, sizing, and sp acing cont rols. Not ice also in t he figur e t hat the Web page appears wit h a gr id. The default set t ing is for cont rols to snap t o the grid m arks as you m ov e controls around t he form . I f you prefer m ore granular positioning or you don’t w ant t o show a gr id, you can achieve t hese result s by choosing Options from t he Tools m enu. I n t he Options dialog box , Open t he HTML Designer folder and select Disp lay. The right port ion of t he dialog box then reveals cont rols for set t ing the Snap To Grid and Show Grid opt ions. This portion of the Opt ions dialog box also includes t ext boxes for you to specify the spacing bet w een grid m ark s in Design v iew. You can also cont rol t he appearance of t he grid by right - click ing any blank area of a page in Design view and ch oosing Pr opert ies. I f t he pageLay out property for a page is GridLayout , t he General t ab of t he DOCUMENT Propert y Pages dialog box exposes t he Show Gr id check box. Clearing t he check box st ops the grid fr om appearing, but cont rols still snap t o t he grid when you m ove t hem . I f you sw it ch from Design v iew t o HTML view, you can see t he form and / form tags inside bounding body and / body t ags. Just as t he form and / form t ags are wit hin body and / body t ags, so are t he tags for t he but t on and label controls wit hin t he t ags for t he form . You can see from t he follow ing HTML excerpt for t he WebForm 1.aspx page t hat the button and label cont r ols are Web server cont rols Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. because t heir init ial t ag begins with t he asp prefix (asp : Button and asp: Label) . Aft er t he t ag designat ion init iat ing a cont r ol, a series of at t r ibut e set t ings defines the cont rol feat ures. The attribut e set t ings are so num erous that t hey cause the cont rol tag to wrap t o a second line. The id at t r ibut e indicat es t he nam e by w hich your pr ogram can refer t o t he cont rol. The TOP at t ribut e designat es how far down, in pixels, a cont rol is from t he t op page border. Not ice that t he label cont rol is 40 pixels farther dow n t he page t han t he but t on. The but t on control is 7 pixels closer t o the page’s left border t han t he label cont r ol. <body MS_POSITIONING="GridLayout"> <form id="Form1” method="post” runat="server"> <asp:Button id="Button1” style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px” runat="server” Text="Button"> </asp:Button> <asp:Label id="Label1” style="Z-INDEX: 102; LEFT: 15px; POSITION: absolute; TOP: 48px” runat="server">Label </asp:Label> </form> </body> Right - click ing any blank area in the Design v iew t hat appears in Figure 11- 2 opens a cont ext m enu from which you can choose View Code. This exposes the Visu al Basic code behind t he form . (See Fig ure 11- 3.) The t ab disp laying t he code behind t he form has t he nam e WebFor m 1.aspx.vb. This is also t he nam e of t he file cont aining t he code behind t he form . The Solution Explor er window t o the right of t he code window sh ows t he WebForm 1.aspx.vb file select ed. You won’t see t his f ile unless you’ve select ed Show All Files on the Solut ion Explorer t oolbar . The declarat ions at t he top of t he m odule w indow indicat e t hat t he code resides in a class inst ance nam ed WebFor m 1, which inherit s pro-perties, m et hods, and event s from the Page nam espace in the Syst em .Web.UI hierarchy. I n addition, the But t on1 and Label1 cont r ols on t he Web page are inst ances of t he But t on and Label classes. The definit ions for t he But t on and Label classes reside in t he Syst em .Web.UI .WebCont rols nam espace. N ot e Recall that y ou can discover the funct ion of controls on a toolbar, such as the Show All Files control, by hovering your cursor over t he control until a descriptive phrase appears. Figur e 1 1 - 3 . The code w indow for th e W ebApplica tion s1 sam ple w it h t h e W e bForm 1 .aspx.vb file sele ct e d in Solution Ex plorer. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. When y ou choose to view t he code for an em pt y Web page, you w ill see j ust one procedur e, t he Page_Load event procedure. This procedur e will cont ain a com m ent t hat reads, “Put user code t o init ialize t he page here.” I ret ained t his com m ent in Figure 11- 3 for y our r eference. The Page_Load ev ent procedure works m uch like t he For m_Load ev ent procedure for Windows applicat ions. You can m ak e assignm ent s that need t o t ak e place before a user interacts wit h a Web page. I n t he WebApplication1 sam ple, t he Page_Load ev ent pr ocedure assigns “Click m e” t o t he Text property for But t on1 and clears t he Text propert y for Label1. The But t on1_Click event procedur e can be included because of t he Prot ect ed Wit hEvent s declaration at t he t op of t he m odule. Creat ing and Running ASP.N ET Solu t ions Building Web solut ions w it h ASP.NET can be very st raight forward for Visual Basic dev elopers. This section includes several sam ples t o illust rat e basic Web design issues. You st art wit h a review of t echniques for com piling and v iewing Web pages. Th is review leads to a sam ple t hat drills down on t echniques for m anaging the appearance of cont rols on a Web page. I n t he process, you learn synt ax issues for t he Page_Load ev ent procedur e as well as event procedur es for cont rols on a Web page. Anot her pair of sam ples dem onst rat es how t o const ruct a m ult ifunct ion calculat or. These samples illust rate t ech niques for working w it h text boxes on a Web page as well as how t o opt im ize pages for different t ypes of browsers. The sect ion concludes wit h a sam ple t hat aut om at ically det ect s the type of t he browser request ing a page and t ransfers cont rol t o a page opt im ized for that brow ser t ype. Com piling a nd View ing W eb Pa ge s I f you inst all t he folder w it h t he WebFor m 1.aspx file for WebApplicat ion1 in t he wwwroot folder for t he Web serv er on your local com puter, you can open t he solut ion in a browser wit h t he following URL: ht t p: / / localhost / WebApplicat ion1/ WebFor m 1.aspx Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. This URL designates t he local Web server by specifying localhost . I f you have t he WebApplicat ion1 folder cont aining WebForm 1.aspx inst alled on a differ ent com put er running I I S and t he .NET Fram ew ork, you can open t he Web page in a browser wit h t he following URL. Serv ernam e typically specifies t he nam e of the com put er running t he Web server. ht t p: / / < server nam e> / WebApplicat ion1/ WebForm 1.aspx I recom m end running pages from a browser. I n fact , if you are sure t hat your applicat ion will have t o be r un by users wit h different browser t ypes, you should test your Web pages w it h as many of t hese browser types as possible. You m ight even consider t rying out your Web pages wit h different sett ings. Som e set t ings can affect how t he page w orks in a browser— part icular ly if the page t aps any client -side (t his m eans browser) funct ionality. I w ill cover exam ples of t hese issues later in t his ch apt er. You m ust com pile any changes to a page before you can v iew t hose changes in a browser. Therefore, for init ial t est ing purposes, you m ight care t o open your Web pages from wit hin Visual Studio so y ou can m ore easily debug y our solutions. You can right - click a page t hat you want t o open in Solut ion Explorer and choose one of t w o m enu it em s to view t he page. I f you haven’t m ade any changes t o t he page since t he last build, you can select View I n Br owser. This opens a Browse window w it hin Visual Studio t hat simulat es t he appearance of the page as if it were in a browser. I f you edit ed t he page’s layout or code behind t he page since the last t im e t he page was viewed, choose Build And Brow se from t he cont ext m enu when you right -click t he page in Solut ion Explorer. Th is recompiles t he solut ion w it h any ch anges that y ou m ade before disp laying t he page wit hin t he Visu al St udio Br owse window . No m at t er which approach you use to open a Browse window, t he window appears on a separate t ab like t he ones for t he Web page’s layout and t he code behind the page. Right -click wit hin t he Browse window for a select ion of com m on br owser com m ands, such as Back, Forward, and Refresh Browser. Figure 11-4 sh ows WebForm 1.aspx fr om WebAp plicat ion1 w hen it init ially opens in I nt er net Explor er and aft er a click of the but t on labeled Click Me. One click of the but t on causes a label below t he but t on t o show Hello World. Repeat edly clicking t he but t on seem s t o hav e no effect , alt hough the Page_ Load and But t on1_Click event procedur es operat e for each click . This is because each click ret ur ns t he Web page back t o t he server for processing. The reason not hing appears t o change is t hat the label’s Text pr operty get s set t o t he sam e value on each ret urn t o t he server as a result of a click of But t on1. Figu r e 1 1 - 4 . The t op w in dow sh ow s how W e bForm 1 .aspx from W e bApplicat ion 1 a ppears w hen it in it ia lly loa ds. Th e bottom w in dow show s t he W eb page aft er a click of t h e bu tt on . Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... ASP.NET for exchanging inform at ion bet ween a Web page and a Web ser ver ASP.NET applicat ions r out inely st ore inform at ion about t he cont rols on a Web page in a v iew st at e field for t he page Micr osoft enables ASP.NET dev elopers t o t ak e advant age of t his built - in field The view st at e field on a Web page updat es at bot h t he br owser and Web- ser ver sides of a page’s round-... ern of a word pr ocessor inst ead of t he classic Visual Basic drag- and- drop st yle I built anot her version of t he WebCalculat or aspx page in t he WebCalculat or proj ect t o dem onst rat e t he st eps for creat ing a calculat or in a t able St art by creat ing a new Web page in t he WebCalculat or proj ect Choose Proj ect , Add Web Form fr om t he Visual St udio m ain m enu in Design v iew I n... HTML coding While t his m ay be so, one m aj or benefit of ASP.NET is t hat it insulat es Visual Basic dev elopers from doing m uch HTML coding when t hey ar e building t heir Web solut ions Tak ing aw ay t his benefit in t he int erest of browser - neut ral applicat ions can r egr ess Web applicat ion code dev elopm ent t o t he spaghet t i- coding st yles com m on in som e ASP applicat ion code At... Session St a t e M a na gem e n t The HTTP pr ot ocol on w hich y ou build ASP.NET solut ions doesn’t ret ain inform at ion about Web pages bet w een successive round- t r ips from t he browser t o t he serv er Therefore, ASP.NET offers var ious solut ions for convey ing inform at ion about pr ior visit s t o a Web server Microsoft uses t he t erm session st at e m anagem ent t o describe t he cont... erat ion” t opic in t he Visual St udio NET docum ent at ion for det ails on set t ing t he m ode for a Session variable in t he code behind a Web page By default , Session var iables are in pr ocess This in- pr ocess set t ing y ields super ior perform ance, alt hough it doesn’t ext end t he scope in t he sam e way t hat out - of- pr ocess st orage does Please purchase PDF Split-Merge on www.verypdf.com... ple, Figur e 1 1- 8 shows t he WebCalculat or.aspx page in a Net scape 4.01 browser Not ice t hat t he page doesn’t look lik e t he w indows in Figur e 1 1- 7 The prim ar y r eason for t he div ergence in appearance is t hat t he Net scape br owser doesn’t support absolut e posit ioning ASP.NET uses t his feat ur e t o enable t he posit ioning of cont rols on a Web page w it h drag- and- dr op t echniques... purchase PDF Split-Merge on www.verypdf.com to remove this watermark Ov e r vie w of Se ssion St a t e M a na ge m e n t I ssu e s I n Web applicat ions, Web serv ers don’t t ypically ret ain dat a on a page bet ween round- t r ips; t his is because of t he serv ers’ use of t he HTTP pr ot ocol Ther efor e, on t wo separat e round- t rips from a browser t o a serv er , t he ser ver has no built - in way t... Session v ariables in pr ocess on a Web serv er as in ASP I n addit ion, you can st or e Session v ariables out of process on a st and- alone rem ot e Windows serv er, such as Window s 2000 Serv er, or on a SQL Ser ver inst ance These lat t er t wo opt ions are an ASP.NET innovat ion The abilit y t o st or e Session variables out of process allows Session var iables t o hav e a scope t hat ext ends... ice specifically t hat no code is m ix ed in w it h t he page Figu re 1 1 - 6 Th e D esign vie w of t h e W e b pa ge t h a t a pp ea r s in t h e fou r diffe r en t st at e s sh ow n in Figu re 1 1 - 5 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Because t he lay out for t he Web page is so basic, t he cont rol of t he page’s appearance m ust be in t he logic of t... propert y of “Web Calculat or” Ar range t he lay out so t hat it looks lik e Figur e 1 1- 9 I achiev ed t his look w it h a sequence of copy - and- past e operat ions fr om t he WebCalculat or aspx page t o t he WebCalculat orI nTable.aspx page No ot her operat ions wer e necessary N ot e Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark I pur posely excluded t he equal sign . ASP .NET is free wit h the II S 5.0 Web server, and t he Internet Explorer browser is free with m ost Microsoft product s and from Microsoft ’s I nt ernet. the Window s St art butt on. Choose Microsoft Visual St udio .NET Docum entat ion from t he Microsoft Visual St udio .NET it em on t he Program s m enu.

Ngày đăng: 24/12/2013, 02:18

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan