.. Strongly typed set:
In the. NET Framework, the namespace System. Collections. Generic contains a set different from the hash table and the Arraylist. Only a single type of objects can be stored, called a Generic set. You can create a strong type set for a generic type set. For a strongly typed collection, you need to select the type of the storage item. during compilation, different types of objects cannot be added, because conversions without types increase the speed of data access to a certain extent.
Data Binding using a strongly typed set:
The Code is as follows:
In the aspx file, drag the corresponding control from the toolbox.
The. aspx. cs file code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Collections;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Create a collection www.2cto.com
List <string> fruit = new List <string> ();
Fruit. Add ("apple ");
Fruit. Add ("banana ");
Fruit. Add ("grape ");
// Define the data source
ListBox1.DataSource = fruit;
DropDownList1.DataSource = fruit;
CheckBoxList1.DataSource = fruit;
RadioButtonList1.DataSource = fruit;
// Bind
This. DataBind ();
}
}
.. Dictionary set
In the. NET Framework, there is a Dictionary class in the namespace System. Collections. Generic that represents a set of keys and values.
To avoid the constant conversion of types, reduce system packing and unpacking, and determine the data type, you can use a Dictionary class, such as a shopping cart that stores user information on an e-commerce website.
Example of data binding in a dictionary set:
The namespace and the aspx file are the same as above.
The code snippet is as follows:
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Create a dictionary set, with each index type Integer. each item is of the string type.
Dictionary <int, string> fruit = new Dictionary <int, string> ();
Fruit. Add (1, "apple ");
Fruit. Add (2, "banana ");
Fruit. Add (3, "grape ");
// Bind the list control
ListBox1.DataSource = fruit;
// Select the fields to display
ListBox1.DataTextField = "Value ";
// Bind
This. DataBind ();
}
}
From jory