473,408 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Datagrid / Postback question (ASP.NET)

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
Nov 15 '05 #1
2 9413
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.

"Craig S" <craig@removethis_birch.net> wrote in message
news:e1**************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #2
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
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
1
by: deepika | last post by:
Hi, I have a datagrid with 200 rows and an iframe adjacent to it. When I click on the row, the details of that row show up in the iframe. Now suppose I click on the 150th row, the postback occurs...
1
by: Michelle Stone | last post by:
Hi all. I have an empty datagrid on my web form. And I add BoundDataColumn(s) to it through code. But after each postback, the columns and rows disappear. As a workabout, I tried to rebind the...
2
by: Bruce W.1 | last post by:
There's something I can't figure out. I added some javascript behavior to my datagrid, just like in this article, except in C# instead of VB: ...
2
by: Mark Rae | last post by:
I've inherited an ASP.NET app and have been asked to fix the following problem with a page which is used to display and/or delete records from a database. When the page loads, it displays a list of...
0
by: DotQuery | last post by:
Hello ASP.NET team : I have a user control that be added into page at runtime , in a postback event handler . I save the information to view state , and load the user control again in...
0
by: Freddie | last post by:
hi, i need a datagrid w/ 2 headers, the datagrid is bound if( ! IsPostBack), Ken Cox kindly shared some code that addes a new header in the ItemDataBound method, so i moved it to Page_Load, and...
2
by: Hardy Wang | last post by:
Hi all, We have DataGrid control in Web Form, our client requires to be able to click anywhere of a row to fire the event same as LinkBotton column is clicked. We we did in ASP.NET 1.1 is in...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
1
by: abellix | last post by:
An updatepanel contains a datagrid, this datagrid has columns generated by code-behind: some columns should have async postback, others should have sync postback. Here a sample to reproduce the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.