ASP. NET data binding (2-2)

Source: Internet
Author: User

.. 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

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.