ASP.NET 2.0 Binding Advanced Tips

xiaoxiao2021-04-08  369

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 Supply of SQL Statement AccessDataSource Access Data Source Controls XMLDataSource XML Data Source Control ObjectDataSource SitleMapDataSource SiteMapDataSource Page Navigation Controls Data Source Controls

2.1 SqlDataSource Key Properties

Name Description Connectionstring Connection Connection Connection Connection Strings SelectCommand Used CommandCommand Used to Perform Insert UpdateCommand Used to Perform Update Command TeleteCommand Used command DataSourceMode Specify DataSet or DataReader (Default)

=

DataSet) ProviderName Specifies the supplier (default)

=

SQL Server .Net Provider

2.2 SqlDataSource supports data cache by the following properties

Attribute Name Description EnableCaching Specifies whether to open the cache (default)

=

False

) Cacheduration specifies how much cacheexpirationPolicy specifies the cache interval is Sliding or absolutecacheKeyDependency makes the cache depend on a specific key value SQLCachedependencency makes the cache depend on a specific database entity

2.3 Parameterization Commands XXXParameter Type Specified Parameter Source

Name Description SelectParameters Specifying Parameters for Query Commands Insert Command Specifying Parameters UpdateParameters for Update Command Specifying Parameters DeleteParameters To Delete Commands FilterParameters for Filter Command Specify parameters

2.4 xxxparameter type

Name Description ControlParameter Specifies a Parameter CookieParameter derived from the control specifies that a Parameter for a cookie FormaMeter specifies a parameter of a form that is derived from a PROFILE parameter QueryStringParameter is set in a parameter Parameter from a query string. Parameter Bind a parameter sessionParameter for the data source Specify a parameter from Session 2.5 Using the ControlParameter example

<

ask: SQLDataSourceId

= "Countries"

Runat

= "Server"

Connectionstring

= "Server = localhost; data = northwind;"

SELECTCOMMAND

= "Select Distinct Country from Customers Order By Country"

/>

<

ask: SQLDataSourceId

= "Customers"

Runat

= "Server"

Connectionstring

= "Server = localhost; data = northwind;"

SELECTCOMMAND

= "Select * from customers where country = @ country"

>

<

SelectParameters

>

<

ASP: ControlParameterName

= "Country"

ControlID

= "MyDropDownList"

PropertyName

= "SELECTEDVALUE"

/>

SelectParameters

>

ASP: SQLDataSource

>

<

ASP: DropDownListID

= "MyDropDownList"

DataSourceID

= "Countries"

DataTextField

= "country"

AutoPostback

= "True"

Runat

= "Server"

/>

<

ASP: DataGridDataSourceId

= "Customers"

Runat

= "Server"

/>

2.7 Research stored procedure example

<

ask: SQLDataSourceId

= "Countries"

Runat

= "Server"

Connectionstring

= "Server = localhost; data = northwind;"

SELECTCOMMAND

= "proc_getcountries"

/>

<

ask: SQLDataSourceId

= "Customers"

Runat

= "Server"

Connectionstring

= "Server = localhost; data = northwind;"

SELECTCOMMAND

= "proc_getcuStomers"

>

<

SelectParameters

>

<

ASP: ControlParameterName

= "Country"

ControlID

= "MyDropDownList"

PropertyName

= "SELECTEDVALUE"

/>

SelectParameters

>

ASP: SQLDataSource

>

<

ASP: DropDownListID

= "MyDropDownList"

DataSourceID

= "Countries"

DataTextField

= "country"

AutoPostback

= "True"

Runat

= "Server"

/>

<

ASP: DataGridDataSourceId

= "Customers"

Runat

= "Server"

/>

CREATE PROCEDURE proc_GetCustomers @ Country nvarchar (32) ASSELECT * FROM CustomersWHERE Country = @CountryGOCREATE PROCEDURE proc_GetCustomersCREATE PROCEDURE proc_GetCountriesASSELECT DISTINCT CountryFROM CustomersORDER BY CountryGO

3.XmldataSource uses XML as a data source

Support cache and XSL conversion, only support query binding, do not support updates

<

ASP: XmlDataSourceID

= "Rates"

DataFile

= "Rates.xml"

Runat

= "Server"

/>

<

ASP: TreeViewID

= "MyTreeView"

DataSourceID

= "Rates"

Runat

= "Server"

/>

3.1 Key properties of XMLDataSource

Name Description DataFile XML Data File TracformFile Continuous Data File Path With XSL Style Defined Data Files EnableCaching Specifies whether to turn on Cache (default = false) XPath XPath expression Use to confirm that the data cacheduration in seconds unit time interval CacheExpirationPolicy Specifies the time interval is sligned Or AbsolutecacheKeyDependency creates a cache depending on a key 4. ObjectDataSource

Bind data from the data component, provide data binding of the middleware, make data access and UI disengage, two data binding methods

SelectMethod, InsertMethod, UpdateMethod, And deleteMethod

You can choose whether to use a cache, you can choose whether to use parameters

4.1 ODS Key Properties

ODS key attributes Name Description InsertParameters Specify insertion method parameter specifies the UpdateParameters update method parameter specifies the delete method DeleteParameters SelectParameters parameter specifies the query method parameter CacheDuration cache interval in seconds, and create a clear ObjectDataSource.SelectMethod based cache data entity can use static units SqlCacheDependency Methods can also use a new instance of a class If you use an instance method: ODS creates a new instance class at each call, you must have a public constructive function using ObjectCreated and ObjectDisposing elements to initialize and revoke functions.

5. Enhanced DataGrid control supports complex data cell types, including CheckBoxfields declares high-customized user interface GridView column types in elements:

Name Description Boundfield Display Database Checkboxfield Display button CheckBoxField Display a Boolean Targeting HyperLinkField Display A Hyperlink TemplateField Display a custom HTML template CommandField Display a query or edit button ImageField Display a picture 6. Conflict decision

First in the victory If the data is changed after the data is changed, the modified failed UpdateCommand structure constitutes a specified conflicTDection

=

"CompareAllValues" to achieve the post-winning victory regardless of whether the data is modified, this modification will successfully updateCommand structure constitutes a specified conflicTDection

=

"OverWriteChanges" is implemented

6.1 Advance in the victory law update

<

ask: SQLDataSourceId

= "EMPLOYEES"

Runat

= "Server"

Connectionstring

= "Server = localhost; data = northwind;"

SELECTCOMMAND

= "SELECT EMPLOYEID, Lastname, Firstnamefrom Employees"

UpdateCommand

= "Update employees set lastname = @ lastname, firstname = @ firstnamewhere employeeid = @ original_employeeid and lastname = @ original_lastnameand firstname = @ original_firstname"

ConflicTdection

= "CompareAllValues"

>

<

UpdateParameters

>

<

ASP: Parameters

= "EMPLOYEEID"

Type

= "INT32"

/>

<

ASP: Parameters

= "Lastname"

Type

= "String"

/>

<

ASP: Parameters

= "firstname"

Type

= "String"

/>

UpdateParameters

>

ASP: SQLDataSource

>

<

ASP: GridViewDataSourceId

= "EMPLOYEES"

Width

= "100%"

Runat

= "Server"

Datakeynames

= "EMPLOYEEID"

AutogenerateEditbutton

= "True"

/>

7. Error Detection Data Update After the Control Call GridView.Rowupdated, DetailsView.ItemUpdated, ETC. Handling "Status" event Modify processing update error <

ask: 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 SqlDataSourceto RethrowThe Exception} else if (E.AffectedRows == 0) {// no exception Was thrown, but no records were update

转载请注明原文地址:https://www.9cbs.com/read-132704.html

New Post(0)