cappi
cappi

Reputation: 11

How to refresh a DataBind() after Postback

I'm working on an asp.net website, which shows news to the user. The news are displayed on the website with a repeater in a usercontrol. Right now I'm trying to delete these News with a Button I've inserted into the ItemTemplate of the Repeater (so the button is created dynamicly):

    <asp:Button ID="DelBtn" runat="server" OnClientClick="if(!confirm('Really want to delete?')){return false;}" OnClick="cmdDeleteNews_Click" CommandArgument='<%# Eval("NewsID") %>'  />

The Delete from the Database is not the problem. But how do I make afterwards the Usercontrol refresh, so the deleted news is not shown anymore on the website? Simply using Java Script to delete the Element is possible but since there will be many Users, who update, insert and delete News on the Website, deadlocks are highly propable. Reloading the Page using window.location.reload() would also be possible but does not really make sense. Because of that, I'm looking for a solution to update the Databind of the News-Repeater on PostBack. But since the DataBind()-Method can not be used on Postback, I hope you have a similar solution for this problem or maybe a workaround. The DataBind() to the News-Repeater is done in Codebehind of the Usercontrol

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Databind on PostBack is not permitted (InvalidPostbackException)
       If Not IsPostBack Then
          NewsRepeater.DataSource = Items
          NewsRepeater.DataBind()
          ContentRepeater.DataSource = Items
          ContentRepeater.DataBind()
       End If
    End Sub

Upvotes: 1

Views: 3809

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460370

Reload the datasource in cmdDeleteNews_Click. Then DataBind the databound controls.

Protected Sub cmdDeleteNews_Click(sender As Object, EventArgs As e)
  ' Delete logic here .... '

  ' Then reload and DataBind all:
  Dim items = GetItems()
  NewsRepeater.DataSource = items 
  NewsRepeater.DataBind()
  ContentRepeater.DataSource = Items
  ContentRepeater.DataBind()
End Sub

Upvotes: 2

Related Questions