Data Source overview and configuration and instance code in Asp. Net

Source: Internet
Author: User

Data Source

Data Binding is divided into two parts: data source and data binding control. The data binding control obtains data through the data source and isolates the data provider and data user through the data source, the data binding control modifies data through data sources, including SqlDataSource, AccessDataSource, ObjectDataSource, LinqDataSource, EntityDataSource, and XmlDataSource.

The most widely used data source in objectceceweb development.

Data Binding Control

Data Binding controls include list data binding controls (DropDownList, RadioButtonList, ListBox, CheckBoxList, BulletedList, etc.) and complex controls (DataGrid, GridView, DetailsView, FormView, ListView, Repeater, DataList, etc, and so on ). Repeater is the most lightweight component that is used most frequently on the Internet front-end. ListView is the master of controls such as GridView, DetailsView, FormView, Repeater, and DataList, therefore, the data binding control mainly introduces Repeater and ListView.

ObjectDataSource

ObjectDataSource is used to use a class as the data source. The TypeName attribute is the full name of the data source class, including DeleteMethod, InsertMethod, SelectMethod, and UpdateMethod) these attributes are the method names for deleting, inserting, querying, and updating data in the class. These methods may have parameters, the parameter value is set through eparameters, UpdateParameters, InsertParameters, and other nested nodes.

ObjectDataSource accesses a self-written common class and Marks [DataObject] on the classDataObjectMethod

It is usually too troublesome to write ObjectDataSource manually. Microsoft is considerate and we can do it through a visual interface. Drag and Drop ObjectDataSource to the interface, and select "configure data source" on the Smart icon in the upper right corner to configure the data source. The data source class is generally the TypeNameAdapter class. Select the class and select next. Select the corresponding methods for obtaining, deleting, updating, and inserting data respectively.

Illustration:

1. Drag ObjectDataSourcesControl. aspx from the Toolbox = Data = ObjectDataSources

2. Click the arrow "configure data source" in the upper-right corner of the control. In the displayed dialog box, select the business object that the data source needs to call, such as the class Business Object BLL. Classes.

3. You can select the business-Layer Methods for adding, deleting, modifying, and querying calls. For example, you can query whether to delete parameters in all non-deleted class getAllClasses (Boolean isDel ).

4. Set the default isDel value to false.

5. Now the data source is configured

6. Drag the DropDownList Data Control in the interface and click "=" in the upper right corner. Select the data source as ObjectDataSources1.

Running result

Supplement:

Q: When I first access this page, the data will be loaded in the drop-down box. If I do not call the getAllClasses method for the second data request, will the data still be displayed in the drop-down box?

Code:

Simple front-end Page Control
Copy codeThe Code is as follows:
<Asp: DropDownList ID = "DropDownList1" runat = "server">
</Asp: DropDownList>
<Input type = "submit" value = "request again"/>

Background Page code
Copy codeThe Code is as follows:
Public partial class ObjectDataSourcesControl: System. Web. UI. Page {
Protected void Page_Load (object sender, EventArgs e ){
// Because the data binding control saves the data in ViewState by default, the data will not be reloaded every time the page is refreshed, and only needs to be loaded for the first time (! IsPostBack)
If (! IsPostBack ){
List <MODEL. Classes> lists = new BLL. Classes (). getAllClasses (false );
// Use code binding to bind any object that implements the IEnumerable interface to the data binding control. For example:
// DropDownList1.DataSource = new object [] {3, 5, 6 };
// DropDownList1.DataBind ();
DropDownList1.DataTextField = "CName ";
DropDownList1.DataValueField = "CID ";
DropDownList1.DataSource = lists;
DropDownList1.DataBind ();
}
}
}

When you click "request again", the background page determines that ispostback = false will not getAllClasses again. Why is there still a value in the page drop-down box?

A: because the page returned by the server contains the LoadState executed before the page lifecycle PageLoad, The LoadState command restores the properties and values of the control submitted by the client to the following page source files in the page control.

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.