我有一个转发器,可以在其中加载一些东西。我还有一个按钮,它有一个 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 评估中写什么以确保转发器数据绑定?