Based on the database-based ASP.NET design, many companies and individuals transfer the original C / S application system to B / S mode, and many controls provided in .NET development environment make database bindings and data operations simple and easy Where the DataGrid control is the most commonly used, in order to implement data in data display web page, the DataGrid control provides the embedded editing button group and the corresponding event response, this article will discuss data modification in DataGrid Technology and provide a more effective data editing method. Data example: SQL> Select * from mjg_tempidno class name result pass ---------------------------- ------ ---- 2003060001 Chinese Database & Sql Blog Articles: "Record the corresponding" Edit "chain, the page will become the edit page shown in Figure 2: The implementation of the above graph is a common application of DataGrid, but it has the following insufficient: 1, no batch full screen modification. Modify which record must first press the "Edit" button to enter the editing state of the record, you must also save the modification with the corresponding "Update" button; 2. After the record enters the modified state, the original content is emptied to modify Reference; 3. Modify the editing box to the TextBox control, it is difficult to perform validity to the input modified value, which is almost unable to provide data inspection and restriction; 4. The page overload refresh is high, the viewer has "shaking "The feeling also increases the server load. After the "Update" button is saved, the page will be refreshed, and the new data results are displayed waiting for the next round of modifications. If we are sorted in this example, the test scores of the third record were modified, After the refresh, it appears in another location, recording a large number of users will feel "shaking" record position transform, if the network (server) is busy, the user's changes will fall into frequent waiting; 5, three event definitions Not least. In this example, the edit button event onEDitCommand, the update button event onupdateCommand and the cancel button event onclecaleCaleCalex need to respond to code design, DataGrid is not automatically completed. How to overcome these defects? Back to the flexible and stable control interface like a C / S? We need to first understand the principles of DataGrid work, the above example of the DataGrid page description code is as follows:
A total of five columns in data binding, the sixth list is the edit button group. The first three columns "Candidates", "Candidate Name" and "Exam" are "Readonly" properties, indicating that the three columns cannot be edited, and the display style does not change; fourth And the "Test Score" and "pass or not" are editable. Once the DataGrid is in editing, this two columns will change by the Label display to the Repinted TextBox Edit box, allowing the user to edit. This principle also results in two modes of display status and editing state. The refresh when switching between the page is inevitable. To change the road to "alternative". The event code in the previous example is not detailed in detail due to the space limit. We will introduce alternative DataGrid modifications, will be modified in the following aspects: 1, the fourth and five columns of allowed editing are changed from the binding column to the template column; 2, the display status of the template column is label style and editing state The TextBox style is combined with DropDownList or Checkbox, which is data display and modification, etc. (TextBox itself is also displayed, editing, but it lacks data restrictions and self-test capabilities), using these controls to limit the range of modification values; 3 , Cancel the Sixth Column Edit button group and the corresponding event, merge into a separate button on the interface, the button's response is to save the current full screen modified data. The following will be described in detail below. First, the binding column is changed to the template column, and the button group of the sixth column is canceled. The interface description code is shown in:
......
ask: BOUNDCOLUMN>
ask: BOUNDCOLUMN>
ask: BoundColumn> 'The first three columns have not changed, still BoundColumn (binding column)
EditItemTemplate> 'The definition of the fourth column editing mode, still uses TextBox asp: templateColumn> 'fourth column changes to TemplateColumn (template column), Edititemtemplate> 'The definition of the fifth column editing mode, still uses TextBox asp: templateColumn> Columns> 'column definition is completed ask: DataGrid> Second, the combined display mode and the editing mode have been adjusted above, only the binding is listed to the conversion of the template column, and the fourth and five columns still exists and edit two modes. The following will replace the fourth column with DropDownList. Checkbox replaces the definition of the fifth column, the description code is as follows: ... The former is a digital value, the latter is a string value, how is the two assign a value? Also in the fifth column, Checkbox's Boolean logic value checked how to be a string value? In fact, we need to make changes on the SQL statement of the value, the program code is as follows: Sub show_data 'on the browsing interface Data Data DIM SQL AS STRING DIM Mycomm As New OracleCommand () SQL = "Select IDNO, Class, Name, Decode (Result, 'You', '0', 'Good', '1', ',', '2 ',' Poor ',' 3 ',' 4 ') AS Result, Decode (Pass,' T ',' True ',' false ') as pass from mjg_temp "Dim myadapter as new oracleDataadapter () myadapter.selectCommand = New OracleCommand (SQL, Session ("Database") 'Session ("Database") is a defined database connection object, stored DIM MyDataSet as new dataset () myadapter.fill (MyDataSet, "MyTable") DataGrid1 in the form of a database connection object defined. DataSource = MyDataSet.Tables ("MyTable"). DefaultView DataGrid1.dataBind 'Binds DataGrid1 to SQL statements to query the result datasets to create. Close () end subjects (Sender As Object , E AS Eventargs) 'page load event definition if not ispostback kiln uit End if End Sub Due to the Oracle platform in the database background, define the corresponding Oracle database control object in the ASP.NET program code; the decode function in the SQL statement It is also for Oracle's query function, which is the effect when the Result field value is 'excellent', 'good', ',', 'poor', Convert the result to '0', '1', '2', '3', converted to '4' when other values; when the pass field value is 't', convert the result to 'True', otherwise the conversion For 'false'. Through this conversion, DropDownList and Checkbox can be bound to the correct parameter value. (Other database platforms also have functions similar to Decode, which is not detailed here). After the above modifications, we will see the following ASP.NET Run interface: "Exam Scores" and "Whether to pass", the initial value is displayed as the other item, and there is no DataGrid's Common Edit button, they still You can modify it at any time and no page overload refresh. The modified data remains on the interface can be continued. The range of modified values is also limited to the range of DropDownList, do not worry about possible data logic errors, interface effects and Operable is also significantly improved. Third, the program to save the modified event is designed. Only save the event program code. Write the code when writing code: "Examination Score" in DataGrid and "Whether to pass" items Value, convert DropDownList's current SELECTEDEX value to the corresponding string value, convert CHECKBOX's current Checked logic value to the corresponding string value 't' or 'f', the candidate code to modify the modification operation of the search condition The program code is as follows the Sub SaveButton_Click (Sender AS Object, E AS Eventargs) DIM STRING_TEM1 AS STRING 'Defines the string variable to store the test score string value DIM STRING_TEM2 AS STRING' Defines whether the string variable is used to store the string values Dim item As DataGridItem 'DataGridItem defined objects, each row dim mycontrol1 as Dropdownlist Datagrid for positioning in the' Dropdownlist objects defined, for each row in the positioning Datagrid Dropdownlist dim mycontrol2 as CheckBox 'Checkbox object definitions, each for positioning Datagrid Checkbox Dim Mycomm as new oracleCommand () MyComm.connection = session ("Database") session ("Database"). Open () for each item in datagrid1.items' This loop handles each row in DataGrid MYCONTROL1 = ITEM .findControl ("DropDownList1") 'FindControl is a DataGrid's row object items method SELECT CASEMONTROL1.SELECTEDINDEX CASE 0 string_tem1 = "excellent" case 1 string_te M1 = "good" case 2 string_tem1 = "" Case 3 string_tem1 = "bad" case 4 string_tem1 = "None" end select 'case judgment to convert the current selection serial number of DropDownList to the corresponding grade string value MyControl2 = ITEM .findControl ("CheckBox1") if mycontrol2.checked the String_tem2 = "t" else string_tem2 = "f" 'IF determines whether to convert the current selection of checkbox into the corresponding whether or not the string T or f mycomm.commandtext = "Update MJG_TEMP SET Result = '"