Asp. NET Hands-on Tutorial 10

Source: Internet
Author: User
Tags bind object model one table sort
asp.net| Tutorial updating data in a SQL database
Updating a database in a Web application is often a tricky business. The DataGrid control provides some built-in support
Makes it easy to update the database. To edit Row Records, the DataGrid supports an integer type of edititemin
The Dex property, which he uses to indicate which row of the table control is editable. When this property is set, the DataGrid corresponds
Line with the input box instead of the label. A value of 1 indicates that there are no editable rows. The ASP.net page can be in the server-side form
Contains the DataGrid control, which accesses editable data through the object model of the DataGrid control.
To determine which line will be edited, you need to receive input from some users to determine which line they will edit.
The DataGrid can contain the EditCommandColumn property, which provides a connection to activate three specific events
: EditCommand, UpdateCommand, and CancelCommand. EditCommandColumn was added to the DataGrid
Column collection, as shown in the following example:
...
Oneditcommand= "Mydatagrid_edit"
Oncancelcommand= "Mydatagrid_cancel"
Onupdatecommand= "Mydatagrid_update"
Datakeyfield= "au_id"
>
On the DataGrid tab, you can bind each command event handle that is activated from the EditCommandColumn. These handles
The DataGridCommandEventArgs parameter allows you to directly access the editable row index values selected from the client. Note
In order for the changes to take effect, you need to rebind the DataGrid, as in the following example:
public void Mydatagrid_edit (Object sender, DataGridCommandEventArgs E) {
Mydatagrid.edititemindex = (int) E.item.itemindex;
Bindgrid ();
}
When editing rows in a DataGrid, EditCommandColumn provides two connections to use: Update
and Cancel. If the client chooses cancel, you only need to set the EditItemIndex to-1. If the client chooses
If you choose Update, you need to execute your update command on the database. When you execute the update command, you need to know that it's being edited
A primary key that corresponds to a record in a database. To support this feature, the DataGrid provides a DataKeyField
property, which you can use to set the field for the primary key. In the event handle bound to UpdateCommand, you can get the D
The Atagrid collection of data keys gets the name of the key. You can use the ItemIndex of the event to index the collection, like the following
Example:
mycommand.parameters["@Id"]. Value = mydatagrid.datakeys[(int) E.item.itemindex]
;
;
After the update event handle finishes, set the EditItemIndex to-1. The following example illustrates this situation.
C # datagrid6.aspx
[Run] | [Source code] There is a problem in the previous example, that is, when editing a row, the primary key field (AU
_ID) also provides a text entry box. Because this value is required to determine which row of records in the database is updated, the
You may not want the client to change this value. Fortunately, you can specify each editable row in detail by specifying the outer
View, to prevent the column from providing a text input box. You use the BoundColumn control to assign data words for each column
section that defines each row in the DataGrid's Column collection. Use this technique to achieve full control of the column, including, of course,
ReadOnly property. For the au_id column, you can set its ReadOnly property to True. This way, when a row is in the edit
The au_id column is still displayed as a text label, not as a text entry box, when the pattern is set. The following example demonstrates the
This technique.
C # datagrid7.aspx
[Run] | [Source code] The BoundColumn control is not the only control in the DataGrid's column collection where the property can be set.
You can also specify TemplateColumn, which provides complete control over the contents of the column. The template shows more of the content
At random, you can provide any control you like in the columns of the DataGrid, as well as server-side controls. Below
Example shows how to use TemplateColumn, use a drop-down list server control on the "State" column, and Cont
Ract column uses the check box HTML control. The ASP.net data-binding syntax is used to output the value of a data field in a template. Note
There is a little bit of skill when it comes to the state of the map Drop-down list and check box in the row.
C # datagrid8.aspx
[Run] | [Source code]
Like placing a Drop-down list box or check box in TemplateColumn, you can also place other controls in it
。 In the following example, a validator validation control is added to check the input of the client before performing the update.
C # datagrid9.aspx
[Run] | [Source code]
Delete data in the SQL database
Deleting records from a database is very similar to updating or inserting commands, but you still need to determine which
A specific line. Another control that can be added to the DataGrid column is ButtonColumn, which simply provides a button
Control. ButtonColumn supports a CommandName property that can be set to ' Delete '. On the DataGrid,
When you perform a delete operation, you need to bind an event handle to the DeleteCommand. In addition, you need to use the number
The key (DataKeys) collection determines which rows are selected by the client. The following example illustrates this process:
C # datagrid10.aspx
[Run] | [Source code]
Sort from SQL database
For any table, it is often required to have the ability to sort the contained data. However, the DataGrid control
The body does not have the function of sorting data. It invokes the event handle by clicking the column heading that the user clicks to sort the data. When
The AllowSorting property of the DataGrid is set to True,datagrid to provide a hyperlink to the column headings, using the
To activate the sort command on the table. You can set a handle on the Onsortcommand property of the DataGrid to handle the user's
Click. The name of the column as the property of the SortExpression, passed to the DataGridSortCommandEventArgs's parameter
Number. This parameter can be used to set the Sort property of the DataView that is bound to the table. Take a look at the code and examples below.
C # datagrid11.aspx
[Run] | [Source code]
When you use the BoundColumn control, you can explicitly set the SortExpression property for each column, like the following
Examples of faces
C # datagrid12.aspx
[Run] | [Source code]
Using the master-from relationship
It is often the case that the data model contains relationships that cannot be represented by just one table. Many times, in the base
In the Web interface, the user selects a row (usually a caption) in the data, and then navigates to the details page
(usually content) that displays the details of the user's selected row. To use the DataGrid to do this work, you need
Add HyperLinkColumn to the column collection. HyperLinkColumn specifies that when the user clicks the hyperlink, it will restart
The detail page of the orientation. You can use the format string syntax to submit field values in this hyperlink, and field values as
Get method to submit a string parameter. The following example illustrates this process.
Datanavigateurlfield= "au_id"
datanavigateurlformatstring= "Datagrid13_details.aspx?id={0}"
text= "Get Details"
/>
In the detail page, you can get the parameters for the submit string and execute a federated (join) query statement from the data
Library to get more information. See the following example:
C # datagrid13.aspx
[Run] | [Source code]
Writing and using Stored procedures
In general, executing a specific query can achieve different execution performance. Using stored procedures can reduce the
The load on the database. Stored procedures are easy to create, and can even be created using SQL statements. The following code establishes a
A simple stored procedure that returns a table:
CREATE Procedure GetAuthors as
SELECT * from Authors
Return
Go
You can also create a stored procedure that accepts parameters, such as:
CREATE Procedure loadpersonalizationsettings (@UserId varchar) as
SELECT * from personalization WHERE userid= @UserId
Return
Go
Using stored procedures from the ASP.net page is just an extension of the SqlCommand object. CommandText is used to replace special
The name of the stored procedure for the query text. You can specify SqlCommand C by setting the CommandType property.
Ommandtext is a stored procedure.
MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
The following example shows the invocation of a stored procedure to populate a dataset.
C # datagrid14.aspx
[Run] | [Source code]
The parameter passing process of a stored procedure is the same as a specific query, see the following example:
C # datagrid15.aspx
[Run] | [Source code]
C # datagrid16.aspx
[Run] | [Source code]


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.