修改以后,再手工赋于新值一次,不然的话,因为EnabledViewState会自动保持页面的状态,所以就无法自动更新数据,请楼主以后直接说问题所在的关键,而不是贴一大堆代码,看得眼睛好花,也好累
你把以下这一段代码写入一个方法中去(如:DataBinds()),然后在更新以后调用
private sub DataBinds() Dim DS As New System.Data.DataSet()
Response.Expires = 0
Cmd.Fill(DS, "myTable") Dim i, j As Integer
Dim tempid As Integer
Dim FormStr As String ID = CInt(Request.QueryString("id")) FormStr = "<table border=0 cellspacing=0 cellpadding=2><tr bgcolor='#DEDEDE'>" & vbNewLine For i = 0 To DS.Tables(0).Columns.Count - 1
FormStr += "<td>" & DS.Tables(0).Columns(i).Caption & "</td>" & vbNewLine
Next FormStr += "<td>操作</td></tr>" & vbNewLine
For i = 0 To DS.Tables("mytable").Rows.Count - 1
FormStr += "<tr>" & vbNewLine
tempid = DS.Tables("mytable").Rows(i).Item("id")
If ID = tempid Then
For j = 0 To DS.Tables("mytable").Columns.Count - 1
If j = 0 Or j = 10 Then
FormStr += "<td>" & DS.Tables("mytable").Rows(i).Item(j) & "</td>" & vbNewLine
Else
FormStr += "<td><input type=text size=10 name='TB_" & DS.Tables("mytable").Columns(j).Caption & "' value='" & DS.Tables("mytable").Rows(i).Item(j) & "'></td>" & vbNewLine
End If
Next
FormStr += "<td><input type=submit name=submit value='提交'></td></tr>" & vbNewLine
RegisterHiddenField("EditID", ID)
Else
For j = 0 To DS.Tables("mytable").Columns.Count - 1
FormStr += "<td>" & DS.Tables("mytable").Rows(i).Item(j) & "</td>" & vbNewLine
Next
FormStr += "<td><a href='WebForm1.aspx?action=edit&id=" & tempid & "'>修改</a></td></tr>" & vbNewLine
End If
Next
FormStr += "</table>" & vbNewLine
table1.InnerHtml = FormStrEnd sub
然后你的.VB文件应该是:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If IsPostBack() Then '提交修改
Dim EditID As Integer
Dim TB_Field, FieldName As String
Dim UpdateSQL As String
EditID = Request.Form("EditID")
UpdateSQL = "update mytable set "
For Each TB_Field In Request.Form
If Left(TB_Field.ToString(), 3) = "TB_" Then
FieldName = Mid(TB_Field.ToString(), 4)
UpdateSQL += FieldName & "='" & Replace(Request.Form(TB_Field.ToString()), "'", "''") & "',"
End If
Next
If Right(UpdateSQL, 1) = "," Then UpdateSQL = Left(UpdateSQL, Len(UpdateSQL) - 1)
UpdateSQL += " where id=" & EditID
Cmd.UpdateCommand = New OleDb.OleDbCommand(UpdateSQL, Conn)
Cmd.UpdateCommand.ExecuteNonQuery()
Response.Redirect("WebForm1.aspx")
Else '显示数据
DataBinds()
End If
End Sub
End Class
你把以下这一段代码写入一个方法中去(如:DataBinds()),然后在更新以后调用
private sub DataBinds() Dim DS As New System.Data.DataSet()
Response.Expires = 0
Cmd.Fill(DS, "myTable") Dim i, j As Integer
Dim tempid As Integer
Dim FormStr As String ID = CInt(Request.QueryString("id")) FormStr = "<table border=0 cellspacing=0 cellpadding=2><tr bgcolor='#DEDEDE'>" & vbNewLine For i = 0 To DS.Tables(0).Columns.Count - 1
FormStr += "<td>" & DS.Tables(0).Columns(i).Caption & "</td>" & vbNewLine
Next FormStr += "<td>操作</td></tr>" & vbNewLine
For i = 0 To DS.Tables("mytable").Rows.Count - 1
FormStr += "<tr>" & vbNewLine
tempid = DS.Tables("mytable").Rows(i).Item("id")
If ID = tempid Then
For j = 0 To DS.Tables("mytable").Columns.Count - 1
If j = 0 Or j = 10 Then
FormStr += "<td>" & DS.Tables("mytable").Rows(i).Item(j) & "</td>" & vbNewLine
Else
FormStr += "<td><input type=text size=10 name='TB_" & DS.Tables("mytable").Columns(j).Caption & "' value='" & DS.Tables("mytable").Rows(i).Item(j) & "'></td>" & vbNewLine
End If
Next
FormStr += "<td><input type=submit name=submit value='提交'></td></tr>" & vbNewLine
RegisterHiddenField("EditID", ID)
Else
For j = 0 To DS.Tables("mytable").Columns.Count - 1
FormStr += "<td>" & DS.Tables("mytable").Rows(i).Item(j) & "</td>" & vbNewLine
Next
FormStr += "<td><a href='WebForm1.aspx?action=edit&id=" & tempid & "'>修改</a></td></tr>" & vbNewLine
End If
Next
FormStr += "</table>" & vbNewLine
table1.InnerHtml = FormStrEnd sub
然后你的.VB文件应该是:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If IsPostBack() Then '提交修改
Dim EditID As Integer
Dim TB_Field, FieldName As String
Dim UpdateSQL As String
EditID = Request.Form("EditID")
UpdateSQL = "update mytable set "
For Each TB_Field In Request.Form
If Left(TB_Field.ToString(), 3) = "TB_" Then
FieldName = Mid(TB_Field.ToString(), 4)
UpdateSQL += FieldName & "='" & Replace(Request.Form(TB_Field.ToString()), "'", "''") & "',"
End If
Next
If Right(UpdateSQL, 1) = "," Then UpdateSQL = Left(UpdateSQL, Len(UpdateSQL) - 1)
UpdateSQL += " where id=" & EditID
Cmd.UpdateCommand = New OleDb.OleDbCommand(UpdateSQL, Conn)
Cmd.UpdateCommand.ExecuteNonQuery()
Response.Redirect("WebForm1.aspx")
Else '显示数据
DataBinds()
End If
End Sub
End Class
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货