我有一个转发器,可以加载一些东西。我还有一个按钮,它有一个SQL命令来删除转发器的所有内容,但不幸的是,当用户按下“删除”按钮后页面重新加载时,这些项目仍然存在。
我为页面加载子尝试了类似的东西:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
bookShelfContainer.DataSource = Nothing
Exit Sub
End If
displayUserBooks()
End Sub
这是转发器的子设备:
Sub displayUserBooks()
Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = conn
conn.Open()
Session("currentuser") = User.Identity.Name
cmd.Parameters.AddWithValue("@currentUser", Session("currentuser"))
cmd.CommandText = String.Format("SELECT TOP 6 booklist.ID, booklist.Title, booklist.Author, booklist.imgurl, bookshelfjunction.BookID FROM booklist INNER JOIN bookshelfjunction ON bookshelfjunction.BookID = booklist.ID WHERE bookshelfjunction.UserName = @currentUser")
bookShelfContainer.DataSource = cmd.ExecuteReader()
bookShelfContainer.DataBind()
conn.Close()
End Sub
删除功能以防万一你需要它:
Protected Sub btnClearBookshelf()
Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = conn
conn.Open()
Session("currentuser") = User.Identity.Name
cmd.Parameters.AddWithValue("@currentUser", Session("currentuser"))
cmd.CommandText = String.Format("DELETE FROM bookshelfjunction WHERE bookshelfjunction.UserName = @currentUser")
cmd.ExecuteNonQuery()
conn.Close()
End Sub
我需要在Page_Load If IsPostBack评估中写什么来确保转发器数据库?
答案 0 :(得分:1)
您没有看到更新,因为您只是在数据库中创建它们,而不是在Repeater控件的数据集中。它保留了您绑定的DataSet,并且您没有使用新数据刷新它。
Protected Sub btnClearBookshelf()
Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = conn
conn.Open()
Session("currentuser") = User.Identity.Name
cmd.Parameters.AddWithValue("@currentUser", Session("currentuser"))
cmd.CommandText = String.Format("DELETE FROM bookshelfjunction WHERE bookshelfjunction.UserName = @currentUser")
cmd.ExecuteNonQuery()
conn.Close()
displayUserBooks()
End Sub
因此,您需要重新获取数据以使您的应用知道更新,然后将新数据集绑定到Repeater以显示新数据。如果您认为由于您的事件而发生这种情况,请记住事件处理程序在Page_Load之后运行。