Overview and configuration of data sources in asp.net and practical techniques for example code _

Source: Internet
Author: User

Data source

Data binding is divided into two parts: data source and data-bound control, data-bound control obtains data through data source, isolates data provider and data consumer through data source, data-bound control modifies data through data source, data source has SqlDataSource, AccessDataSource, ObjectDataSource, LinqDataSource, EntityDataSource, XmlDataSource, etc.

The most widely used data source in Objectdatasourceweb development, as the main introduction

Data-bound controls

Data-bound controls have list data-bound controls (DropDownList, RadioButtonList, ListBox, CheckBoxList, BulletedList, and so on) and complex controls (DataGrid, GridView, DetailsView, FormView, ListView, Repeater, DataList, etc., the GridView are all ListView subsets. Repeater is the most lightweight component, the most used in the Internet foreground, ListView is the GridView, DetailsView, FormView, Repeater, DataList, and other controls of the unification, So data-bound controls mainly introduce repeater and ListView

ObjectDataSource

ObjectDataSource is used to make a class as a data source, the TypeName property is the full name of the data source class, DeleteMethod (Delete method), InsertMethod (new method), SelectMethod (Query method), UpdateMethod (Modify method), and so on a number of properties, for the class to delete, insert, query, update the data method name, these methods may have parameters, the value of the parameter is passed DeleteParameters, UpdateParameters, InsertParameters such as nested nodes set.

ObjectDataSource access to the general class of their own writing, annotated on the class [DataObject] DataObjectMethod

Usually hand-written ObjectDataSource too troublesome, Microsoft is more intimate, we can through the visual interface to complete. Drag and drop the ObjectDataSource onto the interface, and configure it by selecting Configure Data source on the smart flag in the upper-right corner. Data source class General Typenameadapter class, select the class, choose Next, select the corresponding methods to get, delete, UPDATE, insert data

Graphic:

1, from the Toolbox = "data =" objectdatasources dragged into the Objectdatasourcescontrol.aspx interface

2, click on the upper right corner of the control arrow "Configure Data Source" Pop-up dialog box select business object that data source needs to call to the business layer method, such as Class business object BLL. Classes Next

3, you can choose to delete or delete the call to check the business layer method, such as query all the deleted classes getallclasses (Boolean Isdel) with parameters to remove the next set of parameters

4, set the default value of Isdel is False

5, this data source is configured to complete

6, the interface dragged into the DropDownList data control also click on the upper right corner = "Select the data source for the above ObjectDataSources1

Run results

Add:

Q: When I first visited the page, the dropdown box loaded the data if the second time I send a data request does not call the Getallclasses Method page dropdown box also display data?

Code Answer :

Front Page Simple control

Copy Code code as follows:

<asp:dropdownlist ID = "DropDownList1" runat = "server" >
</asp:DropDownList>
<input type= "Submit" value= "Request Again"/>

background page code
Copy Code code as follows:

Public partial class ObjectDataSourcesControl:System.Web.UI.Page {
protected void Page_Load (object sender, EventArgs e) {
Because data-bound controls store data in ViewState by default, they will not reload the data every time the page is refreshed, only the first time you need to load (! IsPostBack)
if (! IsPostBack) {
List <model. Classes > Lists = new BLL. Classes (). Getallclasses (false);
Code binding allows you to bind any object that implements the IEnumerable interface to a data-bound control. Like what:
Dropdownlist1.datasource = new object[] {3, 5, 6};
Dropdownlist1.databind ();
Dropdownlist1.datatextfield = "CName";
Dropdownlist1.datavaluefield = "CID";
Dropdownlist1.datasource = lists;
Dropdownlist1.databind ();
}
}
}

When click on "Request Again" background page to judge Ispostback=false will not again getallclasses that why the page dropdown box still has the value?

A: Because the page returned by the server contains __viewstate that is executed before the page life cycle pageload LoadState is to restore the __viewstate saved control properties and values from the client to the page control. Screenshot of the following page source file

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.