0

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

4

1 回答 1

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

因此,您需要重新获取数据以使您的应用程序了解更新,然后将该新数据集绑定到中继器以显示新数据。如果您认为这会因为您的事件而发生,请记住事件处理程序在 Page_Load 之后运行。

于 2013-05-03T19:39:07.520 回答