上述的程序代码范例中,对于数据编修的功能还不是很完整,无法将记录的修改状况更新回数据源。接下来我们利用EditCommandColumn、TemplateColumn 以及一些事件程序将数据更新的功能完成。
<%@Import Namespace=System.Data.ADO%>
<%@Import Namespace=System.Data%>
<Html>
<Form Runat="Server">
<ASP:DataGrid Id="dgA" AllowPaging="True" PageSize="5"
OnPageIndexChanged="dgA_PageChg" Runat="Server"
PagerStyle-Mode="NumericPages" BorderColor="#808080"
HeaderStyle-Font-Names="Courier New"
HeaderStyle-BackColor="#D1DCEB"
HeaderStyle-HorizontalAlign="Center"
AutoGenerateColumns="False"
OnEditCommand="dgA_ECmd" OnUpdateCommand="dgA_UCmd"
OnCancelCommand="dgA_CCmd" >
<Property Name="Columns">
<ASP:TemplateColumn>
<Template Name="HeaderTemplate">
姓名
</Template>
<Template Name="ItemTemplate">
<ASP:Image ImageUrl="ico7.gif" Runat="Server"/>
<%#Container.DataItem("UserName")%>
</Template>
<Template Name="EditItemTemplate">
<ASP:Image ImageUrl="ico8.gif" Runat="Server"/>
<ASP:TextBox Id="txtUserName"
Text='<%#Container.DataItem("UserName")%>'
Runat="Server"/>
</Template>
</ASP:TemplateColumn>
<ASP:TemplateColumn>
<Template Name="HeaderTemplate">
电话
</Template>
<Template Name="ItemTemplate">
<ASP:Image ImageUrl="ico7.gif" Runat="Server"/>
<%#Container.DataItem("UserTel")%>
</Template>
<Template Name="EditItemTemplate">
<ASP:Image ImageUrl="ico8.gif" Runat="Server"/>
<ASP:TextBox Id="txtUserTel"
Text='<%#Container.DataItem("UserTel")%>'
Runat="Server"/>
</Template>
</ASP:TemplateColumn>
<ASP:TemplateColumn>
<Template Name="HeaderTemplate">
住址
</Template>
<Template Name="ItemTemplate">
<ASP:Image ImageUrl="ico7.gif" Runat="Server"/>
<%#Container.DataItem("UserAdd")%>
</Template>
<Template Name="EditItemTemplate">
<ASP:Image ImageUrl="ico8.gif" Runat="Server"/>
<ASP:TextBox Id="txtUserAdd"
Text='<%#Container.DataItem("UserAdd")%>'
Runat="Server"/>
</Template>
</ASP:TemplateColumn>
<ASP:EditCommandColumn
HeaderText="编辑"
EditText="编辑"
UpdateText="更新"
CancelText="放弃"/>
</Property>
</ASP:DataGrid>
</Form>
<Script Language="VB" Runat="Server">
Dim dscA As ADODataSetCommand=New ADODataSetCommand("Select * From
Members", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\InetPub\wwwroot\CR\Ch08\MyWeb.Mdb")
Dim dsDataSet As DataSet=New DataSet
Dim dtDataTable As DataTable
Sub Page_Load(Sender As Object, e As EventArgs)
If Page.IsPostBack=False Then
BindGrid()
End If
End Sub
Sub BindGrid() '数据系结程序
dscA.FillDataSet(dsDataSet,"Members")
dtDataTable=dsDataSet.Tables("Members")
dgA.DataSource=dtDataTable.DefaultView
Page.DataBind()
End Sub
Sub dgA_PageChg(Sender As Object, e As DataGridPageChangedEventArgs) '
换页程序
BindGrid()
End Sub
Sub dgA_ECmd(Sender As Object, e As DataGridCommandEventArgs) '进入编
辑模式dgA.EditItemIndex=e.Item.ItemIndex BindGrid()
End Sub
Sub dgA_UCmd(Sender As Object, e As DataGridCommandEventArgs) '数据更
新程序
Dim shtR As Short=(dgA.CurrentPageIndex * dgA.PageSize) +
e.Item.ItemIndex
Dim txtTemp As TextBox
dscA.FillDataSet(dsDataSet,"Members") '先将DataTable 从资料
源取回
dtDataTable=dsDataSet.Tables("Members") '为了便利起见而将
dtDataTable 指向目标数据表
txtTemp=e.Item.FindControl("txtUserName") '搜寻
DataListItem 内的控件
dtDataTable.Rows(shtR)("UserName")=txtTemp.Text '将找到
的控制之Text 属性更新回
'相对应的记录中
txtTemp=e.Item.FindControl("txtUserTel")
dtDataTable.Rows(shtR)("UserTel")=txtTemp.Text
txtTemp=e.Item.FindControl("txtUserAdd")
dtDataTable.Rows(shtR)("UserAdd")=txtTemp.Text
dscA.Update(dsDataSet,"Members") '将所作的改变更新回数据源
dgA.EditItemIndex=-1
BindGrid()
End Sub
Sub dgA_CCmd(Sender As Object, e As DataGridCommandEventArgs)
dgA.EditItemIndex=-1
BindGrid()
End Sub
</SCRIPT>
</Html>
Sub dgA_ECmd(Sender As Object, e As DataGridCommandEventArgs)
dgA.EditItemIndex=e.Item.ItemIndex
BindGrid()
End Sub
<Template Name= "EditItemTemplate">
<ASP:Image ImageUrl="ico8.gif" Runat="Server"/>
<ASP:TextBox Id="txtUserAdd"
Text='<%#Container.DataItem("UserAdd")%>'
Runat="Server"/>
</Template>
Sub dgA_UCmd(Sender As Object, e As DataGridCommandEventArgs) '数据更
新程序
Dim shtR As Short=(dgA.CurrentPageIndex * dgA.PageSize) +
e.Item.ItemIndex
Dim txtTemp As TextBox
dscA.FillDataSet(dsDataSet,"Members") '先将DataTable 从资料源
取回
dtDataTable=dsDataSet.Tables("Members") '为了便利起见而将
dtDataTable 指向目标数据表
txtTemp=e.Item.FindControl("txtUserName") '搜寻DataListItem 内的
控件
dtDataTable.Rows(shtR)("UserName")=txtTemp.Text '将找到的控制之
Text 属性更新回
'相对应的记录中
'...程序代码略
dscA.Update(dsDataSet,"Members") '将所作的改变更新回数据源
dgA.EditItemIndex=-1 BindGrid()
End Sub
txtTemp=e.Item.FindControl("txtUserName") '搜寻DataListItem 内的控件
dtDataTable.Rows(shtR)("UserName")=txtTemp.Text '将找到的控制之
Text 属性更新回
'相对应的记录中