forked from it-ebooks/75cheatsheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheat Sheets - Core-ASP.Net.html
1074 lines (1007 loc) · 41.6 KB
/
Cheat Sheets - Core-ASP.Net.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<meta charset="gbk">
<title>Core ASP.NET</title>
<script type="text/javascript">
var uagent = navigator.userAgent.toLowerCase();
if (uagent.search("android") > -1) {
document.write('<link rel="stylesheet" href="../css/refcardz_html_android.css" type="text/css" media="screen">');
}
</script>
<base href="http://refcardz.dzone.com/" />
<h1>Core <span id="main_topic">ASP.NET</span></h1>
<p class="author_name">
By Holger Schwichtenberg
</p>
<h2>ABOUT ASP.NEt</h2>
<p>ASP.NET stands for “Active Server Pages .NET”, however
the full name is rarely used. ASP.NET is a framework for the
development of dynamic websites and web services. It is based
on the Microsoft .NET Framework and has been part of .NET
since Version 1.0 was released in January 2002. The current
version named 3.5 Service Pack 1 was released in August 2008.
The next version, 4.0, is expected to be released at the end of
the year 2009.</p>
<p>This Refcard summarizes the most commonly used core functions
of ASP.NET. You will find this Refcard useful for some of
the most common tasks with ASP.NET, regardless of the version
you are using</p>
<h2>Instalation</h2>
<p>The best development environment for ASP.NET is Microsoft’s
Visual Studio. You can either use the free Visual Web
Developer Express Edition <a href="(http://www.microsoft.com/express/
vwd/">(http://www.microsoft.com/express/
vwd/</a>) or any of the commercial editions of Visual Studio (e.g.
Visual Studio Professional). The latest version that supports
ASP.NET 2.0 and ASP.NET 3.5 is “2008” (internal version:
9.0). The .NET Framework and ASP.NET are part of the setup
of Visual Web Developer Express Edition and Visual Studio.
However, make sure you install Service Pack 1 for Visual Studio
2008, as this will not only fix some bugs but also add a lot of
new features.</p>
<p>ASP.NET needs a server with the HTTP protocol (web server)
to run. Visual Web Developer Express 2005/2008 and Visual
Studio 2005/2008 contain a webserver for local use on your
development machine. The “ASP.NET Development Server”
(ADS) will be used when specifying a “File System” location
when creating your project. Thus, “HTTP” would mean you
address a local or remote instance of Internet Information
Server (IIS) or any other ASP.NET enabled web server. ADS
is a lightweight server that cannot be reached from other
systems. However, there are differences between ADS and
IIS, especially in the security model that makes it sometimes
hard for beginners to deploy a website to the IIS that was
developed with ADS. On the production system you will use IIS
and only install the .NET Framework, because Visual Studio is
not required here.</p>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
If you choose to use Internet Information Server (IIS),
install the IIS on your machine before installing the
.NET Framework or Visual Studio. If you did not follow
this installation order, you may use aspnet_regiis.
exe to properly register ASP.NET within the IIS.
</div>
<h2>ASP.NEt Web Applications</h2>
<p>An ASP.NET application consists of several .aspx files. An
.aspx file can contain HTML markup and special ASP.NET
markup (called Web Controls) as well as the code (Single Page
Model). However, the Code Behind Model which comes with
a separate code file called, the “Code Behind File” (.aspx.
cs or .aspx.vb), provides a cleaner architecture and better
collaboration between Web designers and Web developers.
ASP.NET applications may contain several other elements
such as configuration files (maximum one per folder), a global
application file (only one per web application), web services,
data files, media files and additional code files.</p>
<p>There are two types of Web projects: “Website Projects” (File/
New/Web Site) and “Web Application Projects” (File/New/
Project/Web Application). “Website” is the newer model,
while Web Application Projects mainly exist for compatibility
with Visual Studio .NET 2002 and 2003. This Refcard will only
cover Web Site Projects. Most of this content is also valid for
Web Applications.</p>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
A well designed ASP.NET application distinguishes
itself by having as little code in the Code Behind
files and other code files as possible. The large
majority of your code should be in referenced
Assemblies (DLLs) as they are reusable in other
Web applications. If you don’t want to put your code
into a separate assembly, you at least should use
separate classes in the “App_Code” folder within your
web project.
</div>
<img src="/sites/all/modules/dzone/assets/refcardz/046/images/rc046-010d-Figure1.jpg" width="454" height="324" alt="Figure 1">
<p><small><strong>Figure 1:</strong> The Content of an ASP.NET Web Application</small></p>
<h2>The ASP.NET Web form Model</h2>
<p>ASP.NET uses an object- and event-oriented model for web
pages. The ASP.NET Page Framework analyzes all incoming
requests as well as the .aspx page that the request is aimed
at. The Page Framework creates an object model (alias control
tree) based on this information and also fires a series of events.
Event handlers in your code can access data, call external
code in referenced .NET assemblies and manipulate the object
model (e.g. fill a listbox or change the color of a textbox). After
all event handlers have executed, the Page Framework renders
the current state of the object model into HTML tags with
optional CSS formatting, JavaScript code and state information
(e.g. hidden fields or cookies). After interacting with the page,
the user can issue a new request by clicking a button or a link
that will restart the whole process.</p>
<img src="/sites/all/modules/dzone/assets/refcardz/046/images/rc046-010d-Figure2.jpg" width="453" height="219" alt="Figure 2">
<p><small><strong>Figure 2:</strong> The ASP.NET request/response life cycle</small></p>
<h2>Web Controls</h2>
<p>An ASP.NET page can contain common HTML markup.
However, only ASP.NET web controls provide full objectand
event-based functionality. Web controls have two
representations: In the .aspx files they are tags with the prefix
“asp:”, e.g. <asp:textbox>. In the code they are .NET classes,
e.g. System.Web.UI.WebControls.TextBox.</asp:textbox></p>
<p>Table 1 lists the core members of all web controls that are
implemented in the base class “System.Web.UI.WebControls.
WebControl”.</p>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td class="dark_blue"><strong>Name of Member</strong></td>
<td class="dark_cream"><strong>Description</strong></td>
</tr>
<tr>
<td class="light_blue">Id</td>
<td class="light_cream">Unique identifier for a control within a page</td>
</tr>
<tr>
<td class="light_blue">ClientID</td>
<td class="light_cream">Gets the unique identifier that ASP.NET generates if more
than one control on the page has the same (String) ID.</td>
</tr>
<tr>
<td class="light_blue">Page</td>
<td class="light_cream">Pointer to the page where the control lives</td>
</tr>
<tr>
<td class="light_blue">Parent</td>
<td class="light_cream">Pointer to the parent control, may be the same as “Page”</td>
</tr>
<tr>
<td class="light_blue">HasControls()</td>
<td class="light_cream">True, if the control has sub-controls</td>
</tr>
<tr>
<td class="light_blue">Controls</td>
<td class="light_cream">Collection of sub-controls</td>
</tr>
<tr>
<td class="light_blue">FindControl(“NAME”)</td>
<td class="light_cream">Finds a sub-control within the Controls collection by its ID</td>
</tr>
<tr>
<td class="light_blue">BackColor, BorderColor,<br>
Borderstyle, BorderWidth,<br>
Font, ForeColor, Height,<br>
Width, ToolTip, TabIndex</td>
<td class="light_cream">Self-explaining properties for the formatting of the control.</td>
</tr>
<tr>
<td class="light_blue">CssClas</td>
<td class="light_cream">The name of CSS class that is used for formatting the control</td>
</tr>
<tr>
<td class="light_blue">Style</td>
<td class="light_cream">A collection of single CSS styles, if you don’t want to use a
CSS class or override behavior in a CSS class</td>
</tr>
<tr>
<td class="light_blue">EnableViewState</td>
<td class="light_cream">Disables page-scoped state management for this control</td>
</tr>
<tr>
<td class="light_blue">Visible</td>
<td class="light_cream">Disables rendering of the control</td>
</tr>
<tr>
<td class="light_blue">Enabled</td>
<td class="light_cream">Set to false if you want the control to be disabled in the browser</td>
</tr>
<tr>
<td class="light_blue">Focus()</td>
<td class="light_cream">Set the focus to this control</td>
</tr>
<tr>
<td class="light_blue">DataBind()</td>
<td class="light_cream">Gets the data (if the control is bound to a data source)</td>
</tr>
<tr>
<td class="light_blue">Init()</td>
<td class="light_cream">Fires during initializtion of the page. Last chance to change
basic setting e.g. the culture of the current thread that
determines the behavior used for rendering the page.</td>
</tr>
<tr>
<td class="light_blue">Load()</td>
<td class="light_cream">Fires during the loading of the page. Last to change to do any preparations./td>
</td></tr>
<tr>
<td class="light_blue">PreRender()</td>
<td class="light_cream">Fires after all user defined event handlers have completed<br>
and right before rendering of the page starts. Your last<br>
chance to make any changes to the controls on the page!</td>
</tr>
<tr>
<td class="light_blue">UnLoad()</td>
<td class="light_cream">Event fires during the unloading of a page.</td>
</tr>
</tbody></table>
<p><small><strong>Table 1:</strong> Core Members in the base class system. Web.UI.WebControls.WebControl.</small></p>
<p>Tables 2, 3 and 4 list the most commonly used controls for ASP.
NET web pages. However, there are more controls included
in the .NET platform and many more from third parties not
mentioned here.</p>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td class="dark_blue"><strong>Control</strong></td>
<td class="dark_cream"><strong>Purpose/strong></strong></td>
<td class="dark_blue"><strong>Important specific members in<br>
addition to the members inheritedn<br>
from WebControl</strong></td>
</tr>
<tr>
<td class="light_blue"><asp:Label></td>
<td class="light_cream">Static Text</td>
<td class="light_blue">Text</td>
</tr>
<tr>
<td class="light_blue"><asp:TextBox></td>
<td class="light_cream">Edit Text (single line,<br>
multiline or password)</td>
<td class="light_blue">TextMode, Text, TextChanged()</td>
</tr>
<tr>
<td class="light_blue"><asp:FileUpload></td>
<td class="light_cream">Choose a file for upload</td>
<td class="light_blue">FileName, FileContent, FileBytes,SaveAs()</td>
</tr>
<tr>
<td class="light_blue"><asp:Button></td>
<td class="light_cream">Display a clasic button</td>
<td class="light_blue">Click(), CommdName, Command()</td>
</tr>
<tr>
<td class="light_blue"><asp:ImageButton></td>
<td class="light_cream">Display a clickable image</td>
<td class="light_blue">Click(), CommdName, Command()</td>
</tr>
<tr>
<td class="light_blue"><asp:LinkButton></td>
<td class="light_cream">Display a hyperlink that works like a button</td>
<td class="light_blue">ImageUrl, ImageAlign, Click(),<br>
CommdName, Command()</td>
</tr>
<tr>
<td class="light_blue"><asp:CheckBox></td>
<td class="light_cream">Choose an option</td>
<td class="light_blue">Text, Checked, CheckedChanged()</td>
</tr>
<tr>
<td class="light_blue"><asp:RadioButton></td>
<td class="light_cream">Choose an option</td>
<td class="light_blue">Text, Checked, CheckedChanged()</td>
</tr>
<tr>
<td class="light_blue"><asp:HyperLink></td>
<td class="light_cream">Display a hyperlink</td>
<td class="light_blue">NavigateURL, Target, Text</td>
</tr>
<tr>
<td class="light_blue"><asp:Image></td>
<td class="light_cream">Display an image</td>
<td class="light_blue">ImageURL, ImageAlign</td>
</tr>
<tr>
<td class="light_blue"><asp:ImageMap></td>
<td class="light_cream">Display a clickable<br>
image with different<br>
regions</td>
<td class="light_blue">ImageURL, ImageAlign,</td>
</tr>
</tbody></table>
<p><small><strong>Table 2:</strong> Core controls for ASP.NET web pages.</small></p>
<p>List controls display several items that the user can choose
from. The selectable items are declared static in the .aspx
file or created manually using the Items collection or created
automatically by using data binding. For data binding you can
fill DataSource with any enumerable collection of .NET objects.
DataTextField and DataValueField specify which properties of
the objects in the collection are used for the list control.</p>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
If you bind a collection of primitive types such as
strings or numbers, just leave DataTextField and
DataValueField empty.
</div>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
Setting AppendDataBoundItems to true will add the
databound items to the static items declared in the
.aspx file. This will allow the user to select values that
don’t exist in the data source such as the values “All”
or “None”
</div>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td class="dark_blue"><strong>Control</strong></td>
<td class="dark_cream"><strong>Purpose</strong></td>
<td class="dark_blue"><strong>Important specific members in<br>
addition to the members inherited<br>
from WebControl</strong></td>
</tr>
<tr>
<td class="light_blue"><asp:Drop DownList></td>
<td class="light_cream">Allows the user to select<br>
a single item from a<br>
drop-down list</td>
<td class="light_blue">Items.Add(), Items.<br>
Remove(), DataSource,,<br>
DataTextField, DataValueField,<br>
AppendDataBoundItems,<br>
SelectedIndez, SelectedItem,<br>
SelectedValue, SelectedIndexChanged()</td>
</tr>
<tr>
<td class="light_blue"><asp:ListBox></td>
<td class="light_cream">Single or multiple selection box</td>
<td class="light_blue">Items.Add(), Items.<br>
Remove(), DataSource,,<br>
DataTextField, DataValueField,<br>
AppendDataBoundItems,<br>
SelectedIndez, SelectedItem,<br>
SelectedValue, SelectedIndexChanged(),<br>
Rows, SelectionMode</td>
</tr>
<tr>
<td class="light_blue"><asp:Check BoxList></td>
<td class="light_cream">Multi selection check box group</td>
<td class="light_blue">Items.Add(), Items.<br>
Remove(), DataSource,,<br>
DataTextField, DataValueField,<br>
AppendDataBoundItems,<br>
SelectedIndez, SelectedItem,<br>
SelectedValue, SelectedIndexChanged(),<br>
RepeatLayout, RepeatDirection</td>
</tr>
<tr>
<td class="light_blue"><asp:Radio ButtonList></td>
<td class="light_cream">Single selection radio button group</td>
<td class="light_blue">Items.Add(), Items.<br>
Remove(), DataSource,,<br>
DataTextField, DataValueField,<br>
AppendDataBoundItems,<br>
SelectedIndez, SelectedItem,<br>
SelectedValue, SelectedIndexChanged(),<br>
RepeatLayout, RepeatDirection</td>
</tr>
<tr>
<td class="light_blue"><asp:Bulleted List></td>
<td class="light_cream">List of items in a bulleted format</td>
<td class="light_blue">Items.Add(), Items.<br>
Remove(), DataSource,,<br>
DataTextField, DataValueField,<br>
AppendDataBoundItems,<br>
SelectedIndez, SelectedItem,<br>
SelectedValue, SelectedIndexChanged(),<br>
BulletImageUrl, BulletStyle</td>
</tr>
</tbody></table>
<p><small><strong>Table 3:</strong> List Controls for ASP.NET web pages</small></p>
<p>Validation Controls check user input. They always refer to
one input control ControlToValidate and display a text
ErrorMessage if the validation fails. They perform the checks in
the browser using JavaScript and also on the server. The client
side validation can be disabled by setting EnableClientScript
to false. However, the server side validation cannot be
disabled for security reasons.</p>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td class="dark_blue"><strong>Control</strong></td>
<td class="dark_cream"><strong>Purpose</strong></td>
<td class="dark_blue"><strong>Important specific members<br>
in addition to the members<br>
inherited from WebControl</strong></td>
</tr>
<tr>
<td class="light_blue"><asp:Required<br>
FieldValidator></td>
<td class="light_cream">Checks if a user changed the<br>
initial value of an input control</td>
<td class="light_blue">ControlToValidate, ErrorMessage,<br>
Display, EnableClientScript,<br>
IsValid, InitialValue</td>
</tr>
<tr>
<td class="light_blue"><asp:Compare<br>
Validator></td>
<td class="light_cream">Compares the value entered by<br>
the user in an input control with<br>
the value entered in another<br>
input control, or with a constant<br>
value</td>
<td class="light_blue">ControlToValidate, ErrorMessage,<br>
Display, EnablesClientScript,<br>
IsValid, ValueToCompare, Type,<br>
ControlToCompare</td>
</tr>
<tr>
<td class="light_blue"><asp:Range<br>
Validator></td>
<td class="light_cream">Checks whether the value of<br>
an input control is within a<br>
specified range of values</td>
<td class="light_blue">ControlToValidate, ErrorMessage,<br>
Display, EnableClientScript,<br>
IsValid, MinimumValue,<br>
MaximumValue, Type</td>
</tr>
<tr>
<td class="light_blue"><asp:Regular<br>
Expression
Validator></td>
<td class="light_cream">Checks if the user input<br>
matches a given regular</td>
<td class="light_blue">ControlToValidate, ErrorMessage,<br>
Display, EnavleClientScript,<br>
IsValid, ValidationExpression</td>
</tr>
<tr>
<td class="light_blue"><asp:Custom<br>
Validator></td>
<td class="light_cream">Performs custom checks on the<br>
server and optional also on the<br>
client using JavaScript</td>
<td class="light_blue">ControlTValidate, ErrorMessage,<br>
Display, EnableClientScript,<br>
IsValid, ValidateEmptyText,<br>
Client ValidationFunction,<br>
ServerValidate()</td>
</tr>
</tbody></table>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
For the CustomValidator you can optionally write
a JavaScript function that performs client side validation.
The function has to look like this:
<script type=”text/javascript”>
function ClientValidate(source, args)
{
if (x > 0) // Any condition
{ args.IsValid=true; }
else
{ args.IsValid=false; }
}
</script>
</div>
<h2>The Page Class</h2>
<p>All web pages in ASP.NET are .NET classes that inherit from
the base class “System.Web.UI.Page”. The class Page has
associations to several other objects such as Server, Request,
Response, Application, Session and ViewState (see figure
3). Therefore, developers have access to a wide array of
properties, methods and events within their code. Table 5 lists
the most important members of a Page and its dependent
classes. Please note that the Page class has the class Control in
its inheritance hierarchy and therefore shares a lot of members
with the WebControl class (e.g. Init(), Load(), Controls,
FindControl). However, these members are not repeated here.</p>
<img src="/sites/all/modules/dzone/assets/refcardz/046/images/rc046-010d-Figure3.jpg" width="400" height="297" alt="Figure 3">
<p><small><strong>Figure 3:</strong> Object Model of “System.Web.UI.Page”</small></p>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td class="dark_blue"><strong>Member</strong></td>
<td class="dark_cream"><strong>Description</strong></td>
</tr>
<tr>
<td class="light_blue">Page Title</td>
<td class="light_cream">Title string of the Page</td>
</tr>
<tr>
<td class="light_blue">Page.IsPostBack</td>
<td class="light_cream">True, if page is being loaded in response to a client postback. False if it is being loaded for the first time.</td>
</tr>
<tr>
<td class="light_blue">Page.IsAsync</td>
<td class="light_cream">True, if the page is loaded in an asynchronous request (i.e. AJAX request)</td>
</tr>
<tr>
<td class="light_blue">Page.IsValid</td>
<td class="light_cream">True, if all validation server controls in the current validation group validated successfully</td>
</tr>
<tr>
<td class="light_blue">Page.Master</td>
<td class="light_cream">Returns the MasterPage object associated with this page</td>
</tr>
<tr>
<td class="light_blue">Page.PreviousPage</td>
<td class="light_cream">Gets the page that transferred control to the current page (only available if using Server.Transfer, not available with Response. Redirect)</td>
</tr>
<tr>
<td class="light_blue">Page.SetFocus(Control ControlID)</td>
<td class="light_cream">Sets the browser focus to the specified control (using JavaScript)</td>
</tr>
<tr>
<td class="light_blue">Trace.Write</td>
<td class="light_cream">Writes trace information to the trace log.</td>
</tr>
<tr>
<td class="light_blue">User.Identity.IsAuthenticated</td>
<td class="light_cream">True, if the user has been authenticated.</td>
</tr>
<tr>
<td class="light_blue">User.Identity.AuthenticationType</td>
<td class="light_cream">Type of Authentication used (Basic, NTLM, Kerberos, etc)</td>
</tr>
<tr>
<td class="light_blue">User.Identity.Name</td>
<td class="light_cream">Name of the current user</td>
</tr>
<tr>
<td class="light_blue">Server.MachineName</td>
<td class="light_cream">Name of the computer the web server is running on</td>
</tr>
<tr>
<td class="light_blue">Server.GetLastError()</td>
<td class="light_cream">Gets the Exception object for the last exception</td>
</tr>
<tr>
<td class="light_blue">Server.HtmlEncode(Text)</td>
<td class="light_cream">Applies HTML encoding to a string</td>
</tr>
<tr>
<td class="light_blue">Server.UrlEncode(Pah)</td>
<td class="light_cream">Applies URL encoding to a string</td>
</tr>
<tr>
<td class="light_blue">Server.MapPath(Path)</td>
<td class="light_cream">Maps the given relative path to an absolute path on the web server</td>
</tr>
<tr>
<td class="light_blue">Server.Transfer(Path)</td>
<td class="light_cream">Stops the execution of the current page and starts executing the given page as part of the current HTTP request</td>
</tr>
<tr>
<td class="light_blue">Request.AcceptTypes</td>
<td class="light_cream">String array of cliet-supported MIME accept types.</td>
</tr>
<tr>
<td class="light_blue">Request.Browser</td>
<td class="light_cream">Provides information about the browser</td>
</tr>
<tr>
<td class="light_blue">Request.ClientCertificate</td>
<td class="light_cream">Provides the certificate of the client, if SSL client authentication is used</td>
</tr>
<tr>
<td class="light_blue">Request.Cookies</td>
<td class="light_cream">The list of cookies that the browser sent to the web server</td>
</tr>
<tr>
<td class="light_blue">Request.Form</td>
<td class="light_cream">The name and value of the input fields the browser sent to the web server</td>
</tr>
<tr>
<td class="light_blue">Request.Headers</td>
<td class="light_cream">Data from the HTTP header the browser sent to the web server</td>
</tr>
<tr>
<td class="light_blue">Request.IsAuthenticated</td>
<td class="light_cream">True, if the user is authenticated</td>
</tr>
<tr>
<td class="light_blue">Request.IsSecureConnection</td>
<td class="light_cream">True, if SSL is used</td>
</tr>
<tr>
<td class="light_blue">Request.Path</td>
<td class="light_cream">Virtual path of the HTTP request (without server name)</td>
</tr>
<tr>
<td class="light_blue">Request.QueryString</td>
<td class="light_cream">Name/Value pairs the browser sent as part of the URL</td>
</tr>
<tr>
<td class="light_blue">Request.ServerVariables</td>
<td class="light_cream">Complete list of name/value pairs with information about the server and the current request</td>
</tr>
<tr>
<td class="light_blue">Request.Url</td>
<td class="light_cream">Complete URL of the request</td>
</tr>
<tr>
<td class="light_blue">Request.UrlReferrer</td>
<td class="light_cream">Refering URL of the request (Previous page, the browser visited)</td>
</tr>
<tr>
<td class="light_blue">Request.UserAgent</td>
<td class="light_cream">Browser identification</td>
</tr>
<tr>
<td class="light_blue">Request.UserHostAddress</td>
<td class="light_cream">IP address of the client</td>
</tr>
<tr>
<td class="light_blue">Request.UserLanguages</td>
<td class="light_cream">Preferred languages of the user (determined by browser settings)</td>
</tr>
<tr>
<td class="light_blue">Response.BinaryWrite(bytes)</td>
<td class="light_cream">Writes information to an HTTP response output stream.</td>
</tr>
<tr>
<td class="light_blue">Response.Write(string)</td>
<td class="light_cream">Writes information to an HTTP response output stream.</td>
</tr>
<tr>
<td class="light_blue">Response.WriteFile(string)</td>
<td class="light_cream">Writes the specified file directly to an HTTP response output stream.</td>
</tr>
<tr>
<td class="light_blue">Response.BufferOutput</td>
<td class="light_cream">True if the output to client is buffered</td>
</tr>
<tr>
<td class="light_blue">Response.Cookies</td>
<td class="light_cream">Collection of cookies that shall be sent to the browser</td>
</tr>
<tr>
<td class="light_blue">Response.Redirect(Path)</td>
<td class="light_cream">Redirects a client to a new URL using the HTTP status code 302</td>
</tr>
<tr>
<td class="light_blue">Response.StatusCode</td>
<td class="light_cream">HTTP status code (integer) of the output returned to the client</td>
</tr><tr>
<td class="light_blue">Response.StatusDescription</td>
<td class="light_cream">HTTP status string of the output returned to the client</td>
</tr>
<tr>
<td class="light_blue">Session.SessionID</td>
<td class="light_cream">Unique identifier for the current session (a session is user specific)</td>
</tr><tr>
<td class="light_blue">Session.Item</td>
<td class="light_cream">Gets or sets individual session values.</td>
</tr><tr>
<td class="light_blue">Session.IsCookieless</td>
<td class="light_cream">True, if the ID for the current sessions are embedded in the URL. False, if its stored in an HTTP cookie</td>
</tr><tr>
<td class="light_blue">ViewState.Item</td>
<td class="light_cream">Gets or sets the value of an item stored in the ViewState, which is a hidden field used for state management witin a page</td>
</tr><tr>
<td class="light_blue">Application.Item</td>
<td class="light_cream">Gets or sets the value of an item stored in the application state, which is an applicationscope state management facility</td>
</tr>
</tbody></table>
<p><small><strong>Table 5:</strong> Most important members of the Page class and its associated classes</small></p>
<h2>A Typical Page</h2>
<p>Figure 4 shows the typical content of an .aspx page and Figure
5 the content of a typical code behind class. The sample used
is a registration form with three fields: Name, Job Title and
Email Address.</p>
<pre><code>
<%@ Page Language=”C#” AutoEventWireup=”true”
CodeFile=”PageName.aspx.cs” Inherits=”PageName” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//en”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Registration Page</title>
<link href=”MyStyles.css” rel=”stylesheet” type=”text/css”/>
<style type=”text/css”>
.Headline
{
font-size: large; font-weight: bold;
}
</style>
</head>
<body>
<form id=”c_Form” ruanat=”server”>
<div>
<asp:Label runat=”server” ID=”C_Headline” Text=”Please register:”
class=”Headline”></asp:label>
<p>Name:
<asp:TextBox ID=”C_Name” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”C_NameVal” ControlToValidate=”C_
Name” ruanat=”server” ErrorMessage=”Name required”></
asp:RequiredFieldValidator>
</p>
<p>Job Title:
<asp:DropDownList ID=”C_JobTitle” runat=”server”>
<asp:ListItem Text=”Software Developer” Value=”SD”></
asp:ListItem>
<asp:ListItem Text=”Software Architect” Value=”SA”></
asp:ListItem>
</asp:DropDownList>
</p>
<p>EMail:
<asp:TextBox ID=”C_EMail” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator Id=”C_EMailVal1” ControlToValidate=”C_
EMail” runat=”server” ErrorMessage=”EMail required”></
asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”C_EMailVal2”
ControlToValidate=”C_EMail” runat=”server” ErrorMessage=”Email
not valid” ValidationExpression=”\w+([-+.’]\w+)*@\w+([-.]\
w+)*\.\w+([-.]\w+)*”>
</asp:RegularExpressionValidator>
</p>
<p>
<asp:Button ID=”C_register” runat=”server” Text=”Register”
onclick=”C_Register_Click”/>
</p>
</div>
</form>
</body>
</html>
</code>
</pre>
<p><small><strong>Figure 4:</strong> Typical content of an ASPX file</small></p>
<pre><code>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class PageName : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// If an authenticated users starts using this page,
// use his login name in the name textbox
if (!Page.IsPostBack && Page.User.Identity.IsAuthenticated)
{
this.C_Name.Text = Page.User.Identity.Name;
this.C_Name.Enabled = false;
}
}
protected void C_Register_Click(object sender, EventArgs e)
{
if (Page.IsValid) // if all validation controls succedded
{ // call business logic and
if (BL.Register(this.C_Name.Text, this.C_EMail.Text, this.C_
JobTitle.SelectedValue))
{ // redirect to confirmation page
Response.Redirect(“RegistrationConfirmation.aspx”);
}
else
{ // change the headline
this.C_Headline. = “You are already registered!”;
}
}
}
}
</code>
</pre>
<p><small><strong>Figure 5:</strong> Typical content of a Code Behind file</small></p>
<h2>State Management</h2>
<p>State management is a big issue in web applications as the
HTTP protocol itself is stateless. There are three standard
options for state management: hidden files, URL parameters
and cookies. However, ASP.NET has some integrated
abstractions from these base mechanisms know as View State,
Session State, and Application State. Also, the direct use of
cookies is supported in ASP.NET.</p>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
Disabling the View State (EnableViewState=false in
a control) will significantly reduce the size of the
page sent to the browser. However, you will have to
take care of the state management of the controls
with disabled View State on your own. Some complex
controls will suffer the loss of functionality without
View State.
</div>
<p>The following code snippet shows how to set values for a
counter stored in each of these mechanisms:</p>
<pre><code>
ViewState[“Counter”] = CurrentCounter_Page + 1;
Session[“Counter”] = CurrentCounter_Session + 1;
Application[“Counter”] = CurrentCounter_Application + 1;
Response.Cookies[“Counter”].Value = (CurrentCounter_User +
1).ToString();
Response.Cookies[“Counter”].Expires = DateTime.MaxValue; // no
expiration
</code>
</pre>
<p><small><strong>Figure 6:</strong> Setting Values</small></p>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
When reading value from these objects, you have to
check first if they already exist. Otherwise you will
recieve the exception “NullReferenceExeception:
Object reference not set to an instance of an object.”
</div>
<p>The following code snippet shows how to read the current
counter value from each of these mechanisms: Next Column ----></p>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td class="light_blue">Mechanism</td>
<td class="light_blue">Scope</td>
<td class="light_blue">Lifetime</td>
<td class="light_blue">Base Mechanism</td>
<td class="light_blue">Data Type</td>
<td class="light_blue">Storing Value</td>
<td class="light_blue">Reading Value</td>
</tr>
<tr>
<td class="light_cream"><strong>View State</strong></td>
<td class="light_cream"><strong>Single user on a single page</strong></td>
<td class="light_cream">Leaving the current page</td>
<td class="light_cream">Hidden FIeld “ViewState”</td>
<td class="light_cream">Object (any serializable.NET data type)</td>
<td class="light_cream">Page.ViewState</td>
<td class="light_cream">Page.ViewState</td>
</tr>
<tr>
<td class="light_cream"><strong>Session State</strong></td>
<td class="light_cream">Latest interaction of a single<br>
user with the web page</td>
<td class="light_cream">Limited number of minutes after the last request from <br>
the user</td>
<td class="light_cream">Cookie (“ASPSessionJD...”)or URL Parameter “(S(...))”<br>
plus server side store (local RAM, RAM on dedicated<br>
server or database)</td>
<td class="light_cream">Object. Object must be serializable<br>
if the store is not the local RAM</td>
<td class="light_cream">Page.Session</td>
<td class="light_cream">Page.Session</td>
</tr>
<tr>
<td class="light_cream"><strong>Cookies</strong></td>
<td class="light_cream">A single User</td>
<td class="light_cream">Closing of the browser or dedicated point in time</td>
<td class="light_cream">Cookie</td>
<td class="light_cream">String</td>
<td class="light_cream">Page.Response.Cookies/td>
</td><td class="light_cream">Page.Response.Cookies</td>
</tr>
<tr>
<td class="light_cream"><strong>Application State</strong></td>
<td class="light_cream">All users</td>
<td class="light_cream">Shutting down the web application</td>
<td class="light_cream">Local RAM</td>
<td class="light_cream">Object</td>
<td class="light_cream">Page.Application</td>
<td class="light_cream">Page.Application</td>
</tr>
</tbody></table>
<pre><code>
long CurrentCounter_Application, CurrentCounter_ApplicationLimited,
CurrentCounter_Session, CurrentCounter_Page, CurrentCounter_User;
if (Application[“Counter”] == null) { CurrentCounter_Application =
0; }
else { CurrentCounter_Application = Convert.ToInt64(Application[“C
ounter”]); }
if (Session[“Counter”] == null) { CurrentCounter_Session = 0; }
else { CurrentCounter_Session = Convert.
ToInt64(Session[“Counter”]);}
if (ViewState[“Counter”] == null) { CurrentCounter_Page = 0; }
else { CurrentCounter_Page = Convert.
ToInt64(ViewState[“Counter”]); }
if (Request.Cookies[“Counter”] == null) { CurrentCounter_User = 0; }
else { CurrentCounter_User = Convert.ToInt64(Request.
Cookies[“Counter”].Value); }
</code>
</pre>
<p><small><strong>Figure 7:</strong> Reading Values</small></p>
<h2>Configuration</h2>
<p>All configurations for ASP.NET applications are stored in XMLbased
configuration files with the fixed name “web.config”. In
addition to the configuration files in the application root folder,
subfolders may also contain a web.config that overrides parent
settings. Also, there are the global configuration files machine.
config and web.config in the folder \Windows\Microsoft.NET\
Framework\v2.0.50727\CONFIG that provide some default
settings for all web applications. (Note: v2.0.50727 is still
correct for ASP.NET 3.5!).</p>
<p>Visual Studio and Visual Web Developer create a default root
configuration file in your web project that contains a lot of
internal setting for ASP.NET 3.5 to work properly. Figure 6
shows a fragment from a web.config file with settings that are
often used.</p>
<pre><code>
<!-- Connection strings -->
<connectionStrings>
<add name=”RegistrationDatabase” connectionString=”Data
Source=EO2;Initial Catalog= RegistrationDatabase;Integrated
Security=True” providerName=”System.Data.SqlClient” />
</connectionStrings>
<!-- User defined settings -->
<AppSettings>
<and key=”WebmasterEMail” value=”hs@IT-Visions.de” />
</appSettings>
<system.web>
<!-- Specify a login page -->
<!-- Use the URL for storing the authentication ID if cookies are
not allowed -->
<!-- Set the authentication timeout to 30 minutes -->
<authentication mode=”Forms”>
<forms loginUrl=”Login.aspx” cookieless=”AutoDetect” timeout=”30”>
</forms>
</authentication>
<!-- Deny all unauthorizd access to this application -->
<authorization>
<deny users=”?” />
</authorization>
<!-- Use the URL for storing the session ID if cookies are not
allowed -->
<!-- Set the session timeout to 30 minutes -->
<sessionState cookieless=”AutoDetect” timeout=”30”></sessionState>
<!-- Display custom error pages for remote users -->
<customErrors mode=”RemoteOnly” defaultRedirect=”GenericErrorPage.
htm”>
<error statusCode=”403” redirect=”NoAccess.htm” />
<error statusCode=”404” redirect=”FileNotFound.htm” />
</customErrors>
<!-- Turn on debugging -->
<compilation debug=”false”>
</code>
</pre>
<p><small><strong>Figure 8:</strong> Typical setting in the web.config file.</small></p>
<div class="hot_tip">
<p><img src="/sites/all/modules/dzone/assets/refcardz/046/../images/hot_tip.gif" alt="Hot Tip" width="64" height="64" class="hot_tip_icon"></p>
Please make sure you turn debugging off again
before deploying your application as this decreases
execution performance.
</div>
<h2>Deployment</h2>
<p>ASP.NET applications can be deployed as source code
via the so called “XCopy deployment”. This means you
copy the whole content of the web project folder to the
production system and configure the target folder on the