Asp. NET: Optimizing the editing capabilities of the DataGrid control

Source: Internet
Author: User
Tags add object end eval sql mysql net string
Asp.net|datagrid|datagrid Control | Optimization Although in the above example we have implemented the DataGrid in the online editing function, but if we have been accustomed to C/s structure of the program, you will feel the last instance of the lack of editing: the submission of data frequently, Increased the burden on the server. In this section, we use an example to illustrate the editing capabilities of the optimized DataGrid control, where the technology is to introduce batch update data. A new knowledge introduced is the FindControl method of the control.

Let's look at concrete examples. First, in the Datacon Web project, add a Web Form, named Datagrid_ Sample6.aspx, and then add a DataGrid control, because we do a display template for the DataGrid control, and in order to optimize its editing properties, we make special use of <asp:templatecolumn >< The Itemtemplate></itemtemplate></asp:templatecolumn > Properties Add DropDownList Controls and CheckBox controls. To facilitate instance application and reader understanding, we create a new Teacherinfor.mdb database that contains a teacher datasheet, field type and virtual data as shown in Figures 9.11 and 9.12.


Figure 9.11 Field properties in a teacher datasheet


Figure 9.12 data records in a teacher datasheet

The main HTML code for datagrid_sample6.aspx is as follows:
<body topmargin= "0" ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" ><b> School of Economics and management teacher Information </b>
<asp:datagrid id= "DataGrid1"
runat= "Server" autogeneratecolumns= "False" width= "320px" pagesize= "4"
allowpaging= "True" >
<alternatingitemstyle backcolor= "WhiteSmoke" ></AlternatingItemStyle>
<itemstyle backcolor= "Ghostwhite" ></ItemStyle>
<Columns>
<asp:boundcolumn datafield= "id" headertext= "number" ></asp:BoundColumn>
<asp:boundcolumn datafield= "name" headertext= "name" ></asp:BoundColumn>
<asp:templatecolumn headertext= "Gender" >
<ItemTemplate>
<asp:dropdownlist id= "Sex" runat= "server" selectedindex= ' <%# Cint (DataBinder.Eval (Container, "Dataitem.sex")) %> ' >
<asp:listitem value= "0" > Male </asp:ListItem>
<asp:listitem value= "1" > Women </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "earned degree" >
<ItemTemplate>
<asp:dropdownlist id= "degree" runat= "server" selectedindex= ' <%# CInt (DataBinder.Eval, Container, " Dataitem.degree "))%> ' >
<asp:listitem value= "0" > Bachelor </asp:ListItem>
<asp:listitem value= "1" > Master </asp:ListItem>
<asp:listitem value= "2" > Doctor </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Gender" >
<ItemTemplate>
<asp:dropdownlist id= "title" runat= "Server" selectedindex= ' <%# Cint (DataBinder.Eval, " Dataitem.title "))%> ' >
<asp:listitem value= "0" > Lecturer </asp:ListItem>
<asp:listitem value= "1" > Associate Professor </asp:ListItem>
<asp:listitem value= "2" > Professor </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Marriage No" >
<ItemTemplate>
<asp:checkbox runat=server Checked = ' <%# DataBinder.Eval (Container, "Dataitem.marry")%> ' id= ' marry ' Text = ' marriage No ">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<pagerstyle backcolor= "Lightsteelblue" mode= "NumericPages" ></PagerStyle>
</asp:DataGrid>
<asp:button id= "Button1"
runat= "Server" text= "batch Update" >
</asp:button></FONT>
</form>
</body>

In the above HTML code, note the data binding of the DataGrid control value:

Selectedindex= ' <%# CInt (DataBinder.Eval (Container, "Dataitem.degree"))%> '
The logical code in DataGrid_Sample6.aspx.vb is as follows:
'----Code begin----------
'-omitting a reference to a namespace
Public Class Datagrid_sample6
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' Omit the code generated by the form designer here
#End Region
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
If not IsPostBack Then
GetData ()
End If
End Sub
' Read data
Sub GetData ()
Dim Mycon as Oledb.oledbconnection
ViewState ("constr") = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath (".") + "\teacherinfor.mdb"
' Save connection connection string using Viewsate
Dim myCMD as Oledb.oledbdataadapter
' Declare DataAdapter Object
Dim MySQL as String
' declares the SQL string of command commands
Try
Mycon = New oledb.oledbconnection (ViewState ("Constr"))
' Instantiate the Connection object
MySQL = "Select Id,name, Sex, degree, title,marry from teacher"
' Set up an SQL statement that queries all content in the database
myCMD = New oledb.oledbdataadapter (mysql, mycon)
Dim dt as Data.dataset = New Data.dataset
' Declare a DataSet object, and instance words
myCMD. Fill (DT)
' Populating data, that is, generating the dataset model database in memory
DataGrid1.DataSource = dt. Tables (0)
' Specify a data source for the DATAGRID1 control
Datagrid1.databind ()
' Perform binding
Catch ex as Exception
Response.Write ("program error, information described below:<br>" & Ex.) Message)
Finally
Mycon. Close ()
End Try
End Sub
' Paging event
Private Sub datagrid1_pageindexchanged (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles datagrid1.pageindexchanged
Datagrid1.currentpageindex = E.newpageindex
GetData ()
End Sub
' Batch update process
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim MySQL as String
Dim Sex as DropDownList
Dim degree as DropDownList
Dim title as DropDownList
Dim Marry as CheckBox
Dim mycon as Oledb.oledbconnection = New oledb.oledbconnection (ViewState ("Constr"))
Mycon. Open ()
Dim myCMD as Oledb.oledbcommand
Try
Dim Item as DataGridItem
' Defines the item variable, representing each item item of this DataGrid control
For each item in Datagrid1.items
' Iterate over the item item of DATAGRID1
Sex = Item. FindControl ("Sex")
' Use the FindControl method to instantiate a list control of a gender field
degree = Item. FindControl ("degree")
' Use the FindControl method instance chemical bit field's list control
title = Item. FindControl ("title")
' Use the FindControl method to instantiate a list control of the title field
Marry = Item. FindControl ("Marry")
' Use the FindControl method to instantiate a list control of a marital field
' To assign a value to the MySQL statement, take advantage of the already instantiated control to get its value.
MySQL = "Update teacher set sex= '" & Sex. SelectedValue & "', Degree= '" & degree. SelectedValue & "'," & _
"Marry=" & Marry. Checked & ", Title= '" & title. SelectedValue & "where id=" & Item. Cells (0). Text
myCMD = New oledb.oledbcommand (mysql, mycon)
' Write update
myCMD. ExecuteNonQuery ()
Next
Catch ex as Exception
Response.Write ("program error, information described below:<br>" & Ex.) Message)
Finally
Mycon. Close ()
Response.Write ("Congratulations!" <script>alert. \ nyou have successfully updated the records! ');</script> ")
' Prompt for UPDATE success!
End Try
GetData ()
End Sub
End Class
'------Code End---------

After you save the compilation, the Datagrid_sample6.aspx run as shown in Figure 9.13.


Figure 9.13 Datagrid_sample6.aspx Running effect

Through the above sections of learning, we have a more detailed understanding of the DataGrid control. But we know that the DataGrid control is a very powerful data-bound control, limited to space, only a rough introduction, there are many functions only in practical application, more exploration, more practice, can really grasp. In the next section, we'll come together to learn another data-bound control ――datalist control.

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.