adaam
adaam

Reputation: 3706

Updating ASP.NET repeater data after Postback

I have a repeater that loads a few things into it. I also have a button that has an SQL command to delete all of the contents of the repeater, but unfortunately when the page reloads after the user presses the 'delete' button, the items are still there.

I've tried something like this for the page load sub:

   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

And here is the sub for the repeater:

 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

And the delete function just in case you need it:

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

What do I need to write in the Page_Load If IsPostBack evaluation to make sure that the repeater databinds?

Upvotes: 0

Views: 1552

Answers (1)

Jasmine
Jasmine

Reputation: 4029

You are not seeing your updates because you are only making them in the database, not in the Repeater control's dataset. It holds on to the DataSet that you bound, and you aren't refreshing it with the new data.

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

So, you need to re-get the data to make your app aware of the updates, then bind that new dataset to the Repeater to show the new data. If you thought that would happen because of your events, keep in mind that event handlers run AFTER Page_Load.

Upvotes: 1

Related Questions