tailieunhanh - Editing and Updating Data in a Web Forms DataGrid
[ Team LiB ] Recipe Editing and Updating Data in a Web Forms DataGrid Problem You need to edit complex data using a DataGrid control and update the database with the changes made. Solution Bind the results of a database query to a DataGrid control and update the database with changes made | Team LiB Recipe Editing and Updating Data in a Web Forms DataGrid Problem You need to edit complex data using a DataGrid control and update the database with the changes made. Solution Bind the results of a database query to a DataGrid control and update the database with changes made in the DataGrid by configuring the appropriate properties and events. The schema of table TBL00705 used in this solution is shown in Table 7-7. Table 7-7. TBL0705 schema Column name Data type Length Allow nulls Id int 4 No IntField int 4 Yes StringField nvarchar 50 Yes The Web Forms page sample code defines the DataGrid control with the four columns that it contains Edit or Update Cancel button Delete button Id field IntField field StringField field and the two templates controlling the appearance of data depending on whether the column is being edited EditItemTemplate or ItemTemplate. The static Eval method of the DataBinder class is used to fill the field values in each template. The specifies the container argument for the method which when used in a data grid resolves to . The code for the Web Forms page is shown in Example 7-9. Example 7-9. File asp DataGrid id dataGrid style Z-INDEX 102 LEFT 16px POSITION absolute TOP 56px runat server Width 576px AutoGenerateColumns False PageSize 5 AllowPaging True AllowSorting True AlternatingItemStyle BackColor FFFF99 AlternatingItemStyle HeaderStyle Font-Bold True HeaderStyle Columns asp EditCommandColumn ButtonType LinkButton UpdateText Update CancelText Cancel EditText Edit HeaderStyle Width 35px HeaderStyle asp EditCommandColumn asp ButtonColumn Text Delete CommandName Delete HeaderStyle Width 50px HeaderStyle asp ButtonColumn asp BoundColumn DataField Id ReadOnly True HeaderText ID asp BoundColumn asp TemplateColumn HeaderText Int Field ItemTemplate asp Label runat server Text Container asp Label ItemTemplate EditItemTemplate asp .
đang nạp các trang xem trước