ASP.NET immediately tutorial 10

xiaoxiao2021-03-05  27

Sender: jiajv (light rain ~ Complete yourself), letter area: DOTNET

Title: ASP.NET immediately tutorial 10

Sending station: Sun and Moonlight (May 05, 2003 00:31:31 Monday), station letters

Update data in the SQL database

Updating the database in a web application is often a tricky thing. DataGrid control provides some built-in support

, Make it easy to update the database. To edit the record, DataGrid supports an integer type EditItemin

DEX properties, he used to indicate which line of the table control is editable. After setting this property, the DataGrid corresponds to

Row input box instead of the label. Value -1 indicates that there is no editable row. ASP.NET page can be in the server-side form

Contains the DataGrid control, access the editable data through the object model of the DataGrid control.

In order to determine which line will be edited, you need to receive some users' input to determine which row they will edit.

DataGrid can include the EditCommandColumn property, which provides a connection to activate three specific events

: EditCommand, UpdateCommand, and Cancelcommand. EditCommandColumn is added in DataGrid

In the column collection, as shown in the following example:

...

OneDitCommand = "MyDataGrid_edit"

Oncancelcommand = "MyDataGrid_cancel"

OnupdateCommand = "MyDataGrid_Update"

DataKeyfield = "au_id"

>

On the DataGrid tag, you can bind each command handle that activates from EditCommandColumn. These handles

The DataGridCommandEventArgs parameter allows you to directly access the editable row index value selected from the client. Note

In order to make changes to take effect, you need to resize DataGrid, like this:

Public void myDataGrid_edit (Object sender, DataGridCommandeventArgs e) {

MyDataGrid.edititeMindex = (int) E.Item.ItemIndex;

Bindgrid ();

}

When editing the rows in DataGrid, EditCommandColumn provides two connections available for use: Update

And Cancel. If the client selects Cancel, you only need to set EditIndEx to -1. If the client selects

Select Update, you need to perform your update command to your database. When performing an update command, you need to know the edited

Call the primary key of the record in the database. To support this feature, DataGrid provides a DatakeyField

Attributes can be used to set the fields corresponding to the primary key. In the event handle bound to the UpdateCommand, you can from D

Atagrid's data key collection acquisition key name. You can use the item's itemIndex to index the collection, like the following

example:

MyCommand.Parameters ["@ ID"]. value = mydatagrid.datakeys [(int) E.Item.ItemIndex]

;

;

After the end of the update event handle, set EditIndEx to -1. The following example demonstrates this situation.

C # datagrid6.aspx

[Run] | [Source Code] There is a problem in the previous example, that is when editing a line, the primary key field (Au

_ID) also provides a text input box. Because this value is required to determine which row record in the database is updated,

With you may not want the client to change this value. Fortunately, you can specify each editable outside by detail

View to prohibit this column from providing a text input box. The specific method is to use the BoundColumn control to allocate the data word of each column.

Segment, define each row in the column collection of DataGrid. Using this technique can realize the full control of the column, of course,

Readonly property. For the AU_ID column, you can set its readOrthly property to True. In this way, when a row is

At the time of the series, the AU_ID column is still displayed as a text tag instead of a text input box. The following example demonstrates

This technology.

C # datagrid7.aspx

[Run] | [Source Code] BoundColumn control is not a unique control of the property in the column collection of DataGrid.

You can also specify TemplateColumn, which provides full control over the contents. Template's display of content

With randomness, you can provide any controls you like in the DataGrid column, as well, including server-side controls. the following

The example demonstrates how to use TemplateColumn, use the drop-down list server control to the "State" column, "Cont

RACT Columns Use the checkbox HTML control .aSP.NET data binding syntax is used to output the value of the data field in the template. Note

There is a little skill when you are intended to map the list of mapped drop-down lists and checkboxes.

C # datagrid8.aspx

[Run] | [Source Code]

Like a drop-down list box or check box in TemplateColumn, you can also place other controls therein.

. In the example below, a Validator authentication control is added, checking the client's input before performing the update.

C # datagrid9.aspx

[Run] | [Source Code]

Remove data in the SQL database

Removing a record from the database is very similar to the update or insertion command, but you still need to determine that you want to delete in the table.

Row. Another control that can be added to the DataGrid column is ButtonColumn, which only provides a button

Control. ButtonColumn supports a commandName property that can be set to "delete". On DataGrid,

When you perform a delete operation, you need to bind an event handle to DeleteCommand. In addition, you need to use

The DataKeys collection to determine the line selected by the client. The following example demonstrates this process:

C # datagrid10.aspx

[Run] | [Source Code]

Sort from the SQL database

For any form, it is often required to have the ability to sort the included data. However, DataGrid controls

It does not have data sorting features. It calls the event handle by clicking the column header you want to sort the data. when

DataGrid's allowsorting property is set to true, and DataGrid provides a hyperlink for column headings.

Activate the sort command to the table. You can set your handle to the DataGrid's OnSortCommand property to handle the user's

Click. The name of the column is used as the attribute of sortexpression, passed to DataGridSortCommandeventArgs

number. This parameter can be used to set the sort attribute that binds to the table's DATAVIEW. Please see the following code and examples.

C # datagrid11.aspx

[Run] | [Source Code]

When you use the BoundColumn control, you can set the sortexpression attribute for each column, as if

Instance

C # datagrid12.aspx

[Run] | [Source Code]

Using the main - from the relationship, there is often such a situation: the relationship containing the data model cannot be manifested by only one table. Many times, in base

In the web interface, the user selects a row in the data (usually the title), and then repositions to the Detail page.

(Usually content), display the details of the user selected. In order to use DataGrid to accomplish this job, need

Add HyperLinkColumn to column collection. HyperLinkColumn specifies that when the user clicks the ultra connection, it will be reabled.

Oriented detail page. You can use the formatted string syntax to submit a field value in this hyperlink, field value as a field value.

GET method submits the parameters of the string. The following example demonstrates this process.

DataNavigateURLFIELD = "au_id"

DataNaviGateURLFORMATSTRING = "DATAGRID13_DETAILS.ASPX? ID = {0}"

Text = "get details"

/>

In the detail page, you can get the parameters of the submission string, and perform a Join query statement, from data

Database for details. See the example below:

C # datagrid13.aspx

[Run] | [Source Code]

Writing and using stored procedures

In general, performing a specific query can achieve different execution performance. Use stored procedures to reduce applications

Database load. The stored procedure is easy to create, and even the SQL statement can be used to create. The following code has established one

Simple returns a stored procedure:

Create Procedure GetAuthors AS

Select * from authors

Return

Go

You can also create a stored procedure that can accept parameters, for example:

Create Procedure LoadPersonalizationSettings (@Userid varchar (50)) AS

Select * from personalization where userid = @ userid

Return

Go

The use of stored procedures from the ASP.NET page is just the extension of the SQLCommand object. CommandText is used for replacement

A name of the stored procedure for query the text. You can specify SQLCommand C with the properties of the CommandType

Ommandtext is a stored procedure.

Mycommand.selectcommand.commandtype = commandtype.storedProcedure;

The following example demonstrates the calling process to populate the DataSet.

C # datagrid14.aspx

[Run] | [Source Code]

The parameter transfer process of the stored procedure is the same as a specific query, please see the example below:

C # datagrid15.aspx

[Run] | [Source Code]

C # datagrid16.aspx

[Run] | [Source Code]

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

New Post(0)