I have a ASP .NET Repeater data bound to a DataTable. Everything works fine where I can add rows to the DataTable & Bind()
and the Repeater will gracefully show the newly bound data and delete from the DataTable and Bind()
and the Repeater will remove the data.
However a strange problem exists where when rows are been deleted, the last standing row, although perfectly deleted from the DataTable would still be shown in the web page. It's as if for the last row, the Repeater is not data bound! But I have put break points and analysed that the item is definitely deleted from the DataTable and the Repeater1.DataBound() is called as well. If the page is refreshed, the last item is deleted as well (just as it should).
I was so very desperate that I did this. I know this is stupid but still it makes the Repeater retains the final row when data bound.
if (dtTelephoneNumbers.Rows.Count == 0)
dtTelephoneNumbers.Clear();
So why is deleting everything else works perfectly and deleting the last standing row is faulty? Any ideas?
Here is my delete code.
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "DeleteNumber")
{
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
DataRow toDelete = dtTelephoneNumbers.Rows.Find(new object[] { commandArgs[0], commandArgs[1] });
dtTelephoneNumbers.Rows.Remove(toDelete);
Debug.WriteLine("dtTelephoneNumbers count = " + dtTelephoneNumbers.Rows.Count);
if (dtTelephoneNumbers.Rows.Count == 0)
dtTelephoneNumbers.Rows.Clear();
Repeater1.DataSource = dtTelephoneNumbers;
Repeater1.DataBind();
}
}
Few additional notes that might be helpful: My datatable is a static datatable. If not, the data would be lost when a post back happens.
public static DataTable dtTelephoneNumbers = new DataTable();
And everything happens inside a UpdatePanel
which is inside a Wizard Step
.
UPDATE 1: The data is local only (for now). I collect all information from the user using a wizard and finally add it to the database.
UPDATE 2: Works just fine when used without an UpdatePanel. :( Looks like the UpdatePanel has got something to do with this!
The only way i could handle the situation was to remove the UpdatePanel from the Code and it worked fine with a full post back of the page!