Common asp.net Code techniques (dpc&dwcreference)-

Source: Internet
Author: User
Tags foreach date array count file system hash new features classic asp
One of the things that I personally found frustrating with classic ASP is the difficulty associated with completing many Common web-related tasks. For example, the need to allow web visitors to upload files to the Web server was fairly common for web developers; However, with classic ASP the "way to accomplish" this without much difficulty is through the use of a third-party COM Component. Similarly, common tasks such as sending emails, reading and writing to the Windows Event Log, working with the WEB server ' s file system, and dynamically generating images based on database information were all tricky, if not impossible, without The aid of a COM component.

Thankfully this has all changed with asp.net. Now WEB developers can easily accomplish a plethora of common tasks without "need to" create or buy a third-party compon ENT the robust. NET Framework of the large part to ASP.net being. When you install the "ASP.net software on your computer from the" CD accompanying this book, in addition to the ASP.net NE, the entire. NET Framework would be installed. The. NET Framework consists of hundreds of classes broken down into a number of logical namespaces. These classes provide the methods and properties needed to create powerful Windows applications, from standalone desktop a PPS to Internet applications.

asp.net Web pages can utilize any of this hundreds of classes, giving ASP.net Web pages the power and flexibility that CL Assic ASP developers could only receive with the use of bulky COM components. In this chapter we'll examine many of the new features that were difficult to implement with classic ASP but can be easi Ly performed with a asp.net Web page.

1. Using Collections
Most Modern programming languages provide support for some type of object, can hold a variable number of elements. These objects are referred to as collections, and they can have elements added and removed with ease without have to wor Ry about proper memory allocation. If you have ' ve programmed with classic ASP before, your ' re probably familiar with the Scripting.Dictionary object, a collection Object that references the each element with a textual key. A collection that stores objects into this fashion is known as a hash table.

There are many types of collections in addition to the hash table. Each type of collection is similar into purpose:it serves as a means to store a varying number of elements, providing a ea Sy Way, at a minimum, to add and remove elements. Each different type of collection was unique in it method of storing, retrieving, and referencing its various elements.

The. NET Framework provides a number of collection types for the developer. In fact, a entire namespace, System.Collections, is dedicated to collection types and helper classes. Each of these collection types can store elements of type Object. Because in. NET all primitive data types-string, integers, date/times, arrays, and so on-are derived from the Object class , these collections can literally store anything! For example, your could use a single collection to store a couple of integers, a instance of a classic COM component, a St Ring, a date/time, and two instances of a custom-written. NET component. Most of the examples in this section is collections to house primitive data types (strings, integers, doubles). However, Listing 2.1.8 (which appears in the similarities among the Collection Types "section) illustrates a Collection o F Collections-that is, a collection type so stores entire collections as each of its elements!

Throughout this section we ll examine five collections the. NET Framework offers developers:the, the ArrayList, The SortedList, the Queue, and the Stack. As you study each of these collections, realize this they all have many. For example, the each type of collection can is iterated through element-by-element using a For Each ... Next Loop in VB (or a foreach loop in C #). Each collection type has a number of similarly named functions the Perform tasks. For example, each collection type has-a clear method this removes all elements from the collection, and a Count property T Hat returns the number of elements in the collection. In fact, the last subsection "similarities among the Collection Types" examines "the common traits found among the Collecti On types.

Working with the ArrayList Class
The ' the ' collection we ' ll look in is the ArrayList. With a ArrayList, each item is stored in sequential and is indexed numerically. In our following examples, keep in mind this developer need not worry to himself with memory. With the standard array, the developer cannot easily add and remove elements the without concerning with the size and makeup of the array. With the collections we'll examine in this chapter, this is no longer a concern.

adding, removing, and indexing Elements in a ArrayList
The ArrayList class contains a number of methods for adding and removing Objects from the collection. These include ADD, AddRange, inserts, Remove, RemoveAt, RemoveRange, and clear, all of which we ' ll examine in Listing 2.1.1 . The output is shown in Figure 2.1.

Listing 2.1.1 for sequentially accessed collections, use the ArrayList
1: <script language= "vb" runat= "Server" >
2:
3:sub Page_Load (sender as Object, E as EventArgs)
4: ' Create two arraylists, Aterritories and Astates
5:dim Aterritories as New ArrayList
6:dim Astates as New ArrayList
7:
8: ' Use the Add ' to add ' states of the ' US
9:astates.add ("Alabama")
10:astates.add ("Alaska")
11:astates.add ("Arkansas")
12: ' ...
13:astates.add ("Wyoming")
14:
: ' Build up our list of territories, which includes
All: ' states plus some additional countries
17:aterritories.addrange (astates) ' Add all States
18:aterritories.add ("Guam")
19:aterritories.add ("Puerto Rico")
20:
' We ' like the ' the ' the ' the ' District of Columbia, territory
: ' So we'll explicitly add it to the beginning of the ArrayList
23:aterritories.insert (0, "District of Columbia")
24:
: ' Display all of the territories with a for loop
26:lblterritories.text = "<i>there are" & Aterritories.count & _
: "Territories...</i><br>"
28:
29:dim I as Integer
30:for i = 0 to Aterritories.count-1
31:lblterritories.text = Lblterritories.text & _
32:aterritories (i) & "<br>"
33:next
34:
: ' We can remove objects in one of four ways:
36: ' ... We can remove a specific item
37:aterritories.remove ("Wyoming")
38:
39: ' ... We can remove an element at a specific position
40:aterritories.removeat (0) ' 'll get rid of District
Columbia,
: ' The
43:
"Display all of the" territories with Foreach Loop
45:lblfewerterritories.text = "<i>there are now" & _
46:aterritories.count & "Territories...</i><br>"
47:
48:dim S as String
49:for each s in aterritories
50:lblfewerterritories.text = Lblfewerterritories.text & _
51:s & "<br>"
52:next
53:
We can remove a chunk of elements from the
: ' Array with RemoveRange
56:aterritories.removerange (0, 2) ' 'll get rid of the '
' Two elements
58:
"Display all of the" territories with Foreach Loop
60:lblevenfewerterritories.text = "<i>there are now" & _
61:aterritories.count & "Territories...</i><br>"
62:
63:for each s in aterritories
64:lblevenfewerterritories.text = Lblevenfewerterritories.text & _
65:s & "<br>"
66:next
67:
In: ' Finally, we can clear the ENTIRE array using the ' Clear method
69:aterritories.clear ()
70:end Sub
71:
: </script>
73:
: <body>
A: <b>the territories of the United states:</b><br>
: <asp:label id= "lblterritories" runat= "Server"/>
78:
: <p>
80:
Bayi: <b>after Some working with the Territories arraylist:</b><br>
Id=: <asp:label "lblfewerterritories" runat= "Server"/>
83:
: <p>
85:
: <b>after further working with the Territories arraylist:</b><br>
<asp:label id= "lblevenfewerterritories" runat= "Server"/>
: </body>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.