2.0 binding Advanced Tips

Source: Internet
Author: User
Tags eval expression insert instance method stored procedure example time interval xpath xsl| Advanced | skills

1. Simple Data binding

<! 1.x data binding expression-->
<%# DataBinder.Eval (Container.DataItem, "price")%>
<!--equivalent 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 a data source control that writes the component itself
SiteMapDataSource page navigation control's data source control 2.1 SqlDataSource key properties
Name Description
ConnectionString connection string for connection database
SelectCommand the command used to execute the query
InsertCommand the command used to execute the insert
UpdateCommand command to perform the update
DeleteCommand the command to perform the deletion
DATASOURCEMODE specifies that the data source type is a dataset or DataReader (default = DataSet)
PROVIDERNAME specifies that the vendor (the default = SQL Server. NET provider) 2.2 SqlDataSource supports data caching through the following properties
Property name Description
enablecaching Specifies whether to open the cache (default = False)
CacheDuration Specify how many results are cached
CACHEEXPIRATIONPOLICY Specifies whether the cache interval is sliding or absolute
Cachekeydependency makes caching dependent on a specific key value
SqlCacheDependency makes caching dependent on a specific database entity 2.3 parameterized command xxxparameter type specify parameter source
Name Description
SelectParameters specify parameters for query commands
InsertParameters specify parameters for the insert command
UpdateParameters specify parameters for the update command
DeleteParameters specify parameters for the delete command
FILTERPARAMETERS Specifies the parameter 2.4 xxxparameter type for the filter command
Name Description
ControlParameter Specifies a parameter that originates from the control
COOKIEPARAMETER Specifies a parameter that originates from a cookie
FORMPARAMETER Specifies a parameter that originates from the form
PROFILEPARAMETER Specifies a parameter that originates from profile
QueryStringParameter in a parameter derived from the query string
Parameter bind a parameter to a data source
SessionParameter specifies an argument from session 2.5 using the ControlParameter example <asp:sqldatasourceid= "Countries" Runa t= "Server"
Connectionstring= "Server=localhost;database=northwind;"
selectcommand= "SELECT distinct country from customers order by country"/>
<asp:sqldatasourceid= "Customers" runat= "Server"
Connectionstring= "Server=localhost;database=northwind;"
Selectcommand= "SELECT * FROM Customers WHERE country = @Country" >
<asp:controlparametername= "Country" controlid= "Mydropdownlist"
Propertyname= "SelectedValue"/>
<asp:dropdownlistid= "Mydropdownlist" datasourceid= "Countries"
datatextfield= "Country" autopostback= "true" runat= "Server"/>
<asp:datagriddatasourceid= "Customers" runat= "server"/>2.7 research stored Procedure Example
<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" >
<asp:controlparametername= "Country" controlid= "Mydropdownlist"
Propertyname= "SelectedValue"/>
<asp:dropdownlistid= "Mydropdownlist" datasourceid= "Countries"
datatextfield= "Country" autopostback= "true" runat= "Server"/>
<asp:datagriddatasourceid= "Customers" runat= "Server"/>
CREATE PROCEDURE Proc_getcustomers
@Country nvarchar () as
SELECT * from Customers
WHERE Country = @Country
CREATE PROCEDURE Proc_getcustomers
CREATE PROCEDURE Proc_getcountriesas
From Customers
ORDER BY Country
GO3. XmlDataSource uses XML as a data source
Supports caching and XSL transformations, supports only query bindings, and does not support updates
<asp:xmldatasourceid= "Rates" datafile= "Rates.xml" runat= "Server"/>
Key properties for <asp:treeviewid= "MyTreeView" datasourceid= "Rates" runat= "Server"/>3.1 XmlDataSource
Name Description
DataFile the path to the XML data file
TransformFile data file path with XSL style definition
enablecaching Specifies whether cache is turned on (default = False)
XPath XPath expressions are used to confirm data
CacheDuration time interval in seconds
CACHEEXPIRATIONPOLICY Specifies whether the time interval is sliding or absolute
Cachekeydependency Create cache dependent on a key
4. ObjectDataSource
Data binding from data components, data binding to middleware, data access and UI separation, two ways of data binding
SelectMethod, InsertMethod, Updatemethod,and DeleteMethod
You can choose whether to use the cache or not, and choose whether to use parameters
4.1 ODS Key Attributes
ODS Key Attributes
Name Description
InsertParameters Specify Insert Method parameters
UpdateParameters Specify Update method parameters
DeleteParameters Specify Delete method parameters
SelectParameters Specify Query method parameters
CacheDuration cache interval time in seconds
SqlCacheDependency a cache based on a data entity
Create and clear
Objectdatasource.selectmethod can use a static method, or you can use a new instance of a class
If you use an instance method: The ODS creates a new instance class every time it is invoked must have a public constructor
Use the objectcreated and objectdisposing elements to initialize and Undo functions 5. Enhanced DataGrid control
Supports complex data cell types, including Checkboxfields declaration of highly customizable user interfaces in <Columns> elements
GridView column Type:
Name Description
BoundField display the text taken out of the database
ButtonField Display button
CheckBoxField use check boxes to display a Boolean variable
HyperLinkField Displays a hyperlink
TemplateField displays a custom HTML template
CommandField display a query or edit button
ImageField shows a picture of 6. Conflict determination
First into victory
If the data is changed after it has been removed, the modification fails
UpdateCommand structure constitutes the designation conflictdetection= "CompareAllValues" to achieve
Back into the victory
The modification succeeds regardless of whether the data has been modified
UpdateCommand structure designation conflictdetection= "overwritechanges" to achieve 6.1 first-entry Victory Law 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" >
<asp:parametername= "EmployeeID" type= "Int32"/>
<asp:parametername= "LastName" type= "String"/>
<asp:parametername= "FirstName" type= "String"/>
<asp:gridviewdatasourceid= "Employees" width= "100%" runat= "Server"
Datakeynames= "EmployeeID" autogenerateeditbutton= "true"/>7. Error detection
The event gridview.rowupdated,detailsview.itemupdated,sqldatasource.updated the control calls after the data is updated, etc.
Handling "status" events, whether or not the database exception allows database exceptions to be processed or discarded again, showing how many database rows have been modified
Handling Update Errors
<asp:sqldatasourceid= "Employees" runat= "Server"
Updatecommand= "" onupdated= "Onupdatecomplete" >


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 Sqldatasourceto Rethrowthe exception
else if (e.affectedrows== 0) {
No exception was thrown, but no records were updated,either.
Might want to let the user know 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: 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.