<%@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-Font-Bold="True"
HeaderStyle-HorizontalAlign="Center"
AutoGenerateColumns="False"
OnEditCommand="dgA_ECmd"
OnUpdateCommand="dgA_UCmd"
OnCancelCommand="dgA_CCmd" >
<Property Name="Columns">
<ASP:BoundColumn
HeaderText="姓名"
DataField="UserName"/>
<ASP:BoundColumn
HeaderText="电话"
DataField="UserTel"/>
<ASP:EditCommandColumn
HeaderText="编辑"
ButtonType="PushButton"
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
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")
dgA.DataSource=dsDataSet.Tables("Members").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)
BindGrid()
Dim shtR As Short=(dgA.CurrentPageIndex * dgA.PageSize) +
dgA.EditItemIndex
Dim txtTemp As TextBox
txtTemp=e.Item.Cells(0).Controls(0) '取回第一个储存格中的
TextBox
'将数据填回对应的DataTable 中
dsDataSet.Tables("Members").Rows(shtR)("UserName")=txtTemp.Te
xt
txtTemp=e.Item.Cells(1).Controls(0) '取回第二个储存格中的
TextBox
'将数据填回对应的DataTable 中
dsDataSet.Tables("Members").Rows(shtR)("UserTel")=txtTemp.Tex
t
'将DataTable 的数据更新回数据源
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 BindGrid()
dscA.FillDataSet(dsDataSet,"Members")
dgA.DataSource=dsDataSet.Tables("Members").DefaultView
Page.DataBind()
End Sub
Sub dgA_UCmd(Sender As Object, e As DataGridCommandEventArgs)
BindGrid()
Dim shtR As Short=(dgA.CurrentPageIndex * dgA.PageSize) +
dgA.EditItemIndex
Dim txtTemp As TextBox
txtTemp=e.Item.Cells(0).Controls(0) '取回第一个储存格中的
TextBox
'将数据填回对应的DataTable 中
dsDataSet.Tables("Members").Rows(shtR)("UserName")=txtTemp.Text
txtTemp=e.Item.Cells(1).Controls(0) '取回第二个储存格中的TextBox
'将数据填回对应的DataTable 中
dsDataSet.Tables("Members").Rows(shtR)("UserTel")=txtTemp.Text
'将DataTable 的数据更新回数据源
dscA.Update(dsDataSet,"Members")
dgA.EditItemIndex=-1
BindGrid()
End Sub
Dim txtTemp As TextBox
txtTemp=e.Item.Cells(0).Controls(0)
dsDataSet.Tables("Members").Rows(shtR)("UserName")=txtTemp.Text
所以要取得显示使用者名称的TextBox 控件之内存地址,以下列叙述即可:
txtTemp=e.Item.Cells(0).Controls(0)
而假设要取回「放弃」TextBox 的内存地址,并将其Text 属性显示出来;那么只要写成下列程序代码片段即可:txtTemp=e.Item.Cells(2).Controls(1)
Response.Write(txtTemp.Text)
dsDataSet.Tables("Members").Rows(shtR)("UserName")=txtTemp.Text
将所有的字段更新完毕后,最后只要利用DataSetCommand 对象的Update 方法将数据更新回数据源即可。
文章来源于领测软件测试网 https://www.ltesting.net/