- 热门文章:
- · Common ASP.NET Code Techniques (DPC&dwc Reference)--2
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--3
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--4
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--5
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--6
- · 在datagrid中删除时确定(精华区的补充)
- · Displaying ListView items - with class! by Rob Birdwell
- · 一个带checkbox的webcontrol
- · 保护 XML Web 服务免受黑客攻击 [第一部分]
- · 保护 XML Web 服务免受黑客攻击, [第二部分]
- · treeview的源代码
- · 在datagrid中删除时确定?(转)
上一篇:如何在DataGrid控件中隐藏列 >>
Common ASP.NET Code Techniques (DPC&DWCReference)--1
Thankfully this has all changed with ASP.NET. Now Web developers can easily accomplish a plethora of common tasks without the need to create or buy a third-party component thanks in large part to ASP.NET being part of the robust .NET Framework. When you install the ASP.NET software on your computer from the CD accompanying this book, in addition to the ASP.NET engine, the entire .NET Framework will 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 apps to Internet applications.
ASP.NET Web pages can utilize any of these hundreds of classes, giving ASP.NET Web pages the power and flexibility that classic ASP developers could only receive with the use of bulky COM components. In this chapter we will examine many of the new features that were difficult to implement with classic ASP but can be easily performed with an ASP.NET Web page.
1. Using Collections
Most modern programming languages provide support for some type of object that 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 having to worry about proper memory allocation. If you@#ve programmed with classic ASP before, you@#re probably familiar with the Scripting.Dictionary object, a collection object that references each element with a textual key. A collection that stores objects in 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 in purpose: It serves as a means to store a varying number of elements, providing an easy way, at a minimum, to add and remove elements. Each different type of collection is unique in its method of storing, retrieving, and referencing its various elements.
The .NET Framework provides a number of collection types for the developer to use. In fact, an 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, you could use a single collection to store a couple of integers, an instance of a classic COM component, a string, a date/time, and two instances of a custom-written .NET component. Most of the examples in this section use 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 of collections—that is, a collection type that stores entire collections as each of its elements!
Throughout this section we@#ll examine five collections the .NET Framework offers developers: the ArrayList, the Hashtable, the SortedList, the Queue, and the Stack. As you study each of these collections, realize that they all have many similarities. For example, each type of collection can be 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 that perform the same tasks. For example, each collection type has a Clear method that removes all elements from the collection, and a Count property that 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 collection types.
Working with the ArrayList Class
The first type of collection we@#ll look at is the ArrayList. With an ArrayList, each item is stored in sequential order and is indexed numerically. In our following examples, keep in mind that the developer need not worry himself with memory allocation. With the standard array, the developer cannot easily add and remove elements without concerning himself with the size and makeup of the array. With all the collections we@#ll examine in this chapter, this is no longer a concern.
Adding, Removing, and Indexing Elements in an ArrayList
The ArrayList class contains a number of methods for adding and removing Objects from the collection. These include Add, AddRange, Insert, 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 method to add the 50 states of the US
9: aStates.Add("Alabama")
10: aStates.Add("Alaska")
11: aStates.Add("Arkansas")
12: @# ...
13: aStates.Add("Wyoming")
14:
15: @# Build up our list of territories, which includes
16: @# all 50 states plus some additional countries
17: aTerritories.AddRange(aStates) @# add all 50 states
18: aTerritories.Add("Guam")
19: aTerritories.Add("Puerto Rico")
20:
21: @# We@#d like the first territory to be the District of Columbia,
22: @# so we@#ll explicitly add it to the beginning of the ArrayList
23: aTerritories.Insert(0, "District of Columbia")
24:
25: @# Display all of the territories with a for loop
26: lblTerritories.Text = "<i>There are " & aTerritories.Count & _
27: "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:
35: @# 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) @# will get rid of District
41: @# of Columbia,
42: @# the first element
43:
44: @# 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:
54: @# ... we can remove a chunk of elements from the
55: @# array with RemoveRange
56: aTerritories.RemoveRange(0, 2) @# will get rid of the
57: @# first two elements
58:
59: @# 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:
68: @# Finally, we can clear the ENTIRE array using the clear method
69: aTerritories.Clear()
70: End Sub
71:
72: </script>
73:
74: <html>
75: <body>
76: <b>The Territories of the United States:</b><br>
77: <asp:label id="lblTerritories" runat="server" />
78:
79: <p>
80:
81: <b>After some working with the Territories ArrayList:</b><br>
82: <asp:label id="lblFewerTerritories" runat="server" />
83:
84: <p>
85:
86: <b>After further working with the Territories ArrayList:</b><br>
87: <asp:label id="lblEvenFewerTerritories" runat="server" />
88: </body>
89: </html>
相关文章:
- · vs.net beta 2中利用DataGrid分页详解
- · COM组件对象与.NET类对象的相互转换
- · 关于DataGrid对象的属性设置(VB)
- · XmlNodeList
- · 用 FormsAuthentication.SetAuthCookie 做权限验证
- · 在datalist中选取数据。
- · Finding a Control Inside a Template
- · DataBinding DropDownList
- · 用asp.net向其他服务器post一条信息
- · Asp.net中用核选框显示数据的方法及ButtonColumn的使用方法
- · 如何在DataGrid控件中实现编辑、删除、分类以及分页操作
- · .NET框架类览胜( Ccident Net )
- · 使用JScript.NET创建asp.net页面(一)
- · 用Visual C#获得计算机名称和IP地址(转)
- · 据说可能是介绍 web.config 最详细的文章。大家参考参考[转]
- · 在ASP.NET中使用Session与Application 对象
- · 用asp.net实现将上传的图片变小存入数据库!(暑假里就开始想做的,很兴奋了)
- · 在ASP.NET中使用Session与Application 对象(续)
- · web.config 关于HttpHandlers 和HttpModules的使用实例【转】
- · 使用 sqlserver来存放和取得 session
- · session 和 viewstate 的比较
- · Online CPU Console using a Web Control Library with .NET Security(3)
- · Online CPU Console using a Web Control Library with .NET Security(2)
- · Online CPU Console using a Web Control Library with .NET Security(1)
- · 实现由web.config控制的验证
- · Passport 你的网站(在你的WebSite上实现MS Passport )上
- · Passport 你的网站(在你的WebSite上实现MS Passport )下
- · 利用ASP.NET来访问Excel文档
- · 如何把存储在数据库中的图片根据自己的需要的大小显示出来。【转】
- · Office XP Charting Examples in asp.net
- · 自定义控件的使用二----DBGlobal.cs部分
- · 自定义控件的使用例子一
- · 任意在datagrid里面添加控件。
- · 三、ASPNET中实现在线用户检测(使用后台守护线程)
- · 二、ASPNET中实现在线用户检测(使用后台守护线程)
- · ASPNET中实现在线用户检测(使用后台守护线程)
- · 通过自定义类来达到向aspx页面加入脚本代码的例子
- · 一个实现自动求和/合并单元格/排序的DataGrid
