Advanced Skills on binding ASP. net2.0

Source: Internet
Author: User
1. Simple data binding
  
<! -- ASP. NET 1.x data binding expression -->
<% # Databinder. eval (container. dataitem, "price") %>
<! -- Equivalent ASP. NET 2.0 data binding expression -->
<% # Eval ("price") %>
<! -- XML Data Binding -->
<% # XPath ("price") %>

2. Data Source Control

Control name control description
Sqldatasource all data source controls that support SQL statements
Accessdatasource Access Data Source Control
Xmldatasource XML Data Source Control
Objectdatasource compile the data source control of the component by yourself
Data Source Control of The sitemapdatasource page navigation control

2.1 key attributes of sqldatasource

Name Description
Connectionstring connection string
Selectcommand
Insertcommand is used to execute the INSERT command
Updatecommand is used to execute the update command.
Deletecommand is used to execute the DELETE command
Datasourcemode specifies that the data source type is dataset or datareader (default = dataset)
Providername specifies the supplier (default value = SQL Server. net provider)

2.2 sqldatasource supports data caching using the following attributes:

Attribute Name Description
Enablecaching specifies whether to enable the cache (default value = false)
Cacheduration specifies the number of cached results.
Cacheexpirationpolicy specifies whether the cache interval is sliding or absolute
Cachekeydependency enables the cache to depend on a specific key value
Sqlcachedependency: enables the cache to depend on a specific database entity.

2.3 parameterized command xxxparameter type specified parameter Source

Name Description
Selectparameters specifies parameters for the query command
Insertparameters specifies parameters for the INSERT command
Updateparameters specifies parameters for the update command
Deleteparameters specifies parameters for the DELETE command
Filterparameters specifies parameters for the filter command

2.4 xxxparameter type

Name Description
Controlparameter specifies a parameter from the control
Cookieparameter specifies a cookie-based parameter.
Formparameter specifies a form-Based Parameter
Profileparameter specifies a parameter from Profile
Querystringparameter is specified as a parameter from the query string.
Parameter binds a parameter to the data source.
Sessionparameter specifies a parameter originating from the session

2.5 example of using controlparameter

<Asp: sqldatasourceid = "countries" runat = "server"
Connectionstring = "Server = localhost; database = northwind ;"
Selectcommand = "select distinct country from MERs order by country"/>
<Asp: sqldatasourceid = "customers" runat = "server"
Connectionstring = "Server = localhost; database = northwind ;"
Selectcommand = "select * from MERs where country = @ country">
<Selectparameters>
<Asp: controlparametername = "country" controlid = "mydropdownlist"
Propertyname = "selectedvalue"/>
</Selectparameters>
</ASP: sqldatasource>
<Asp: dropdownlistid = "mydropdownlist" performanceid = "countries"
Datatextfield = "country" autopostback = "true" runat = "server"/>
<Asp: datagriddatasourceid = "customers" runat = "server"/>

2.6 survey Stored Procedure examples

<Asp: sqldatasourceid = "countries" runat = "server"
Connectionstring = "Server = localhost; database = northwind ;"
Selectcommand = "proc_getcountries"/>
<Asp: sqldatasourceid = "customers" runat = "server"
Connectionstring = "Server = localhost; database = northwind ;"
Selectcommand = "proc_getcustomers">
<Selectparameters>
<Asp: controlparametername = "country" controlid = "mydropdownlist"
Propertyname = "selectedvalue"/>
</Selectparameters>
</ASP: sqldatasource>
<Asp: dropdownlistid = "mydropdownlist" performanceid = "countries"
Datatextfield = "country" autopostback = "true" runat = "server"/>
<Asp: datagriddatasourceid = "customers" runat = "server"/>
Create procedure proc_getcustomers
@ Country nvarchar (32)
Select * from MERs
Where Country = @ country
Go
Create procedure proc_getcustomers
Create procedure proc_getcountriesas
Select distinct country
From MERs
Order by country
Go3.xmldatasource uses XML as the data source

Supports caching and XSL conversion. Only query binding is supported, and update is not supported.

<Asp: xmlperformanceid = "rates" datafile = "rates. xml" runat = "server"/>
<Asp: treeviewid = "mytreeview" performanceid = "rates" runat = "server"/>

3 key attributes of xmldatasource

Name Description
Datafile XML data file path
Transformfile contains the path of the data file defined in the XSL style
Enablecaching specifies whether to enable cache (default value = false)
The XPath expression is used to confirm data.
Cacheduration time interval in seconds
Cacheexpirationpolicy specifies whether the time interval is sliding or absolute
Cachekeydependency creating a cache depends on a key

4. objectdatasource

Bind data from the data component and bind data to the middleware to remove data access from the UI.

Selectmethod, insertmethod, updatemethod, and deletemethod

You can choose whether to use the cache or whether to use parameters.

4.1 Key ODS attributes

ODS key attributes

Name Description
Insertparameters specifies the insert method parameter
Updateparameters specifies the update method parameter
Deleteparameters
Selectparameters
The cache interval of cacheduration is measured in seconds.
Sqlcachedependency is based on the cache of a data entity

Create and clear

Objectdatasource. selectmethod can use a static method or a new instance of the class.

If you use the instance method: ODS must have a common constructor to create a new instance class each time it is called.

You can use objectcreated and objectdisposing elements to initialize and revoke functions.

5. Enhanced DataGrid Control

Supports complex data cell types, including the highly customizable user interface defined in <columns> elements of checkboxfields

Type of the gridview column:

Name Description
Boundfield: displays the extracted text in the database.
Buttonfield display button
Checkboxfield uses check boxes to display a Boolean variable
Hyperlinkfield displays a hyperlink
Templatefield displays a custom HTML Template
Commandfield displays a query or edit button
Imagefield displays an image 6. Conflict Determination

6. Win First

If the data is changed after being retrieved, the modification fails.

The updatecommand structure is composed of the specified conflictdetection = "compareallvalues" to achieve the final victory.

Whether the data is modified or not, the modification is successful.

The updatecommand structure is constructed by specifying conflictdetection = "overwritechanges ".

6.1 first-in-victory rule update

<Asp: sqldatasourceid = "employees" runat = "server"
Connectionstring = "Server = localhost; database = northwind ;"
Selectcommand = "select employeeid, lastname, firstnamefrom employees"
Updatecommand = "Update employees set lastname = @ lastname, firstname =
@ Firstnamewhere employeeid = @ original_employeeid and lastname =
@ Original_lastnameand firstname = @ original_firstname"
Conflictdetection = "compareallvalues">
<Updateparameters>
<Asp: parametername = "employeeid" type = "int32"/>
<Asp: parametername = "lastname" type = "string"/>
<Asp: parametername = "firstname" type = "string"/>
</Updateparameters>
</ASP: sqldatasource>
<Asp: gridviewperformanceid = "employees" width = "100%" runat = "server"

Datakeynames = "employeeid" autogenerateeditbutton = "true"/>

7. Error Detection

The event gridview. rowupdated, detailsview. itemupdated, sqldatasource. Updated, etc.

When a "status" event is handled, no matter whether the database is abnormal or whether the database exception is allowed to be handled or discarded again, how many database rows are modified

Handling update errors
<Asp: sqldatasourceid = "employees" runat = "server"
Updatecommand = "" onupdated = "onupdatecomplete">

</ASP: sqldatasource>

Void onupdatecomplete (Object source, sqldatasourcestatuseventsargse)
{
If (E. Exception! = NULL ){
// Exception thrown. set E. exceptionhandledto true to prevent
// The sqldatasourcefrom throwing an exception, or leave it set
// To false to allow sql1_ceto rethrowthe exception
}
Else if (E. affectedrows = 0 ){
// No exception was thrown, but no records were updated, either.
// Might want to let the user know that the update failed
}
}

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.