在ASP技术作Web编程的时候,因为对数据库的操作使用的RecordSet对象,如果不使用第三方控件,想要做到在线编辑数据就很困难。而DataGrid控件就支持了在线编辑的功能,只要把EditCommandColumn属性设置适当,稍加编程就可以实现了。 DataGrid控件的EditItemIndex属性表示编辑按钮的类别,ASP.NET默认的EditItemIndex=-1,即不支持编辑属性。下面我们通过实例来学习一下。

    (相关文章:ASP.NET:数据库邦定控件入门
    (相关文章:ASP.NET:数据库邦定控件DataBind方法
    (相关文章:ASP.NET:DataGrid控件的分页功能
    (相关文章:ASP.NET:DataGrid控件的排序功能

    在DataCon  Web项目里添加一个Web 窗体,命名为DataGrid_Sample5.aspx,并添加一个DataGrid控件。DataGrid控件属性设置如下:

<asp:DataGrid ></PagerStyle>
</asp:DataGrid>

    在这个实例中,我们使用了自定义的DataGrid控件的模板设置,在设置列时使用<Columns><asp:BoundColumn>标记,其中<asp:BoundColumn>的DataField属性表示绑定的数据表中的字段名称,而HeaderText表示显示的字段名称,这样可以作出友好的数据表头。当使用自定义模板时候,记得一定要把AutoGenerateColumns的值设为"False",否则,在显示的数据表中将重复显示两边,一个是自定义的模板形式的,一个是系统根据数据表显示的。系统默认的是绑定的列可以进行编辑的,但是实际应用中,我们往往不需要每列的字段都可以编辑,只要我们在每个<asp:BoundColumn>里添加ReadOnly="True"就可以屏蔽掉该列的可编辑属性。在这个实例中,为了便于显示,我们只要求"name"字段可以编辑,其他都是ReadOnly="True"。<asp:EditCommandColumn>的ButtonType有两种,分别是PushButton(按钮样式)和LinkButton(超连接样式),系统默认的是LinkButton,我们在这个实例中使用BushButton样式。<asp:ButtonColumn Text="删除" CommandName="Delete">的样式也是上面两种,我们选择的是LinkButton样式 。


我们再来看DataGrid_Sample5.aspx.vb中的逻辑编码部分:
'----code begin-----
'----省略命名空间的引用
Public Class DataGrid_Sample5
    Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
    '此处省略窗体设计器生成的代码
#End Region
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            getdata()
            '调用数据绑定过程
        End If
End Sub
    '下面是数据绑定过程
    Sub getdata()
        Dim mycon As OleDb.OleDbConnection
        Try
            mycon = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(".") + "\StudentInfor.mdb")
            Dim mycmd As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select id ,name,sex,class,address from student", mycon)
            Dim dt As Data.DataSet = New Data.DataSet
            mycmd.Fill(dt)
            DataGrid1.DataSource = dt.Tables(0).DefaultView
            DataGrid1.DataBind()
        Catch ex As Exception
            Response.Write("程序出现错误,信息描述如下:<br>" & ex.Message.ToString)
        Finally
            mycon.Close()
        End Try
End Sub
    '翻页事件过程
 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
    '请求排列顺序事件过程
 Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
        viewstate("sort") = e.SortExpression.ToString
        getdata()
End Sub
    '切换到更新状态 
 Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        getdata()
        '刷新数据
End Sub
    '取消编辑状态
 Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
        DataGrid1.EditItemIndex = -1
        '切换到正常状态
        getdata()
        '刷新数据
End Sub
    'DataGrid1_UpdateCommand事件过程是.NET框架运行时托管的,当按下更新按钮时,就执行更新数据过程
 Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        Dim id As String = e.Item.Cells(0).Text.ToString
        '这里是获取当前更改的记录ID值,
        'e.Item.Cells(0).Text返回的是DataGrid表格中第一列的值()
        Dim name As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
        '这里是获取当前更改的后的值, CType(e.Item.Cells(1).Controls(0),
        'TextBox).Text是先将e.Item.Cells(1).Controls(0)转转成TextBox控件类型,
        '然后获取它的Text值()
        Dim sql As String
        sql = "update  student set name='" + name + "'  where  )
        mycon.Close()
        getdata()
End Sub
End Class
'---cdoe end----------

    在上面的几个Sub过程中, DataGrid1_UpdateCommand过程是个难点 ,其中,我们为了获取编辑记录的ID编号,使用e.Item.Cells(0).Text.ToString来获取,e表示传入的对象,Item表示数据行,Cell表示单元格,Text表示值。为了获取编辑后的新值,我们使用CType(e.Item.Cells(1).Controls(0), TextBox).Text语句,其中e.Item.Cells(1).Controls(0)表示单元格的控件,使用CType转化为TextBox,然后我们就可以获取其Text值,这就是更新的值。在获取了记录的ID编号和更新后的值,我们就可以编写SQL的UPDATE语句进行更新数据。

    将DataGrid_Sample5.aspx保存编译后,运行效果如图9.10所示。

转自it168: DataGrid控件编辑,更新,删除的功能
图9.10 DataGrid_Sample5.aspx运行结果

相关文章:

  • 2021-06-14
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2021-12-23
  • 2022-01-12
相关资源
相似解决方案