Datagrid / Postback question (ASP.NET)

  • Thread starter Thread starter Craig S
  • Start date Start date
C

Craig S

I believe the problem is definately a lack of understanding on my part about
how postbacks work, but here goes...

In my Page_Load method I bind my data from a SQL database to a datagrid -
works great... I've now created a button and in the OnClick method I do a
"DELETE FROM tablename WHERE keyfield=" +variablename (deletes several
records based on which checkboxes are checked in the datagrid).

The problem is that when the page posts back, the DELETE happens in the
database, but it obviously happens after the SELECT statement that populates
the datagrid. I check the DB contents and the records were indeed deleted,
but the datagrid is always one postback behind (If I post the page back
AGAIN, it will get the update)

What code can I add to make sure the datagrid gets bound AFTER the delete
command happens?

Thanks in advance!
Craig
 
Try....

create a method which binds the data to the grid.

in page load add
if(!Page.IsPostBack)
{
<run databinding method>
}

then in your event run the code which deletes the data and then run the
databind method again here.

Dont know if this would be the correct way to do it, but it should work
fine.
 
the problem is that the page_load event is fired before your Delete
event


the workaround is to put the code to populate your page overriding the
pre-render event which happens after the page events are handled


So if you have a DataList dl and DataTable dt , move your code from
the page_load event to the following event


protected override void OnPreRender(EventArgs e)
{
--- add code to populate the datatable
dl.DataSource = dt.DefaultView;
dl.DataBind();

}
}


hope this helps
Fred
 
Back
Top