473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Datagrid & Page_Load

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 the 26 letters of the
alphabet as hyperlinks which the users then click to return records
beginning with that letter, displayed in a DataGrid. The first column of the
DataGrid contains a LinkButton which, when clicked, calls the Delete method
of the DataGrid which deletes the record. Simple enough.

However, when the LinkButton is clicked to delete the record, it obviously
does a PostBack, which populates the DataGrid BEFORE the record is deleted.

The Page_Load is as follows:

private void Page_Load(object sender, System.EventArgs e)
{
// create the 26 hyperlinks
GetHyperlinks();

if(Page.IsPostBack)
{
// fill the DataGrid according to which hyperlink was clicked
Populate(LetterClicked);
}
}

Currently, the delete code simply calls the Populate function once the
record has been deleted, but that means that the code is being called twice,
once in the Page_Load and once in the delete code.
Is there any way to "wrap" the Populate(LetterClicked) line in Page_Load so
that it won't run if the PostBack has been caused by the user clicking a
Delete LinkButton rather than one of the letter hyperlinks? I saw a site
which suggested adding Attributes to the LinkButtons which populate a hidden
field client-side which could be checked server-side, but there must surely
be a better way than this?
Nov 18 '05 #1
2 2197
One of the biggest problem I see with apps is a misunderstanding of
Page_Load. As an event, it used to load the page. This means, Page_Load is
not used for postback unless it affects EVERY postback.

When you post back you have another event to set up the page, so you do not
have to put code in Page_Load. If you do, you are going contrary to the MS
event driven model. Unfortunately, almost every ASP.NET book out there
suggests code like this:

if(!Page.IsPostback)
{
//Set up initial state of page
}
else
{
if(submitState==1)
{
//Do a lot of other stuff
}

...

if(submitState==n)
{
//Do a lot of other stuff
}
}

Since you have postback events for each control, you can simply have this:

if(!Page.IsPostback)
{
//Call routines for initial page load
}
else
{
//Rarely used
//Only if EVERY postback sets up page the same
}

Following this pattern, do the following:

1. Move the DataSet load to its own routine with a routine to pull back the
DataSet:

private void BindPage(string filter)
{
DataGrid1.DataSource=GetDataForPage(null);
}

//Note you can add morefilter conditions to this
private DataSet GetDataForPage(string filter)
{
//Get the dataset and return
//filter will either be null (all records) or A-Z

//NOTE: This can also be done with a DataView
// on the full DataSet
}

3. Move the reload to each button routine

public void btnDelete_Submit(object o, EventArgs e)
{
//Run delete first
//Rebind the page (can add filter, if you are saving it)
BindPage();
}
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside the box!
*************************************************
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:es*************@tk2msftngp13.phx.gbl...
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 the 26 letters of the
alphabet as hyperlinks which the users then click to return records
beginning with that letter, displayed in a DataGrid. The first column of
the DataGrid contains a LinkButton which, when clicked, calls the Delete
method of the DataGrid which deletes the record. Simple enough.

However, when the LinkButton is clicked to delete the record, it obviously
does a PostBack, which populates the DataGrid BEFORE the record is
deleted.

The Page_Load is as follows:

private void Page_Load(object sender, System.EventArgs e)
{
// create the 26 hyperlinks
GetHyperlinks();

if(Page.IsPostBack)
{
// fill the DataGrid according to which hyperlink was clicked
Populate(LetterClicked);
}
}

Currently, the delete code simply calls the Populate function once the
record has been deleted, but that means that the code is being called
twice, once in the Page_Load and once in the delete code.
Is there any way to "wrap" the Populate(LetterClicked) line in Page_Load
so that it won't run if the PostBack has been caused by the user clicking
a Delete LinkButton rather than one of the letter hyperlinks? I saw a site
which suggested adding Attributes to the LinkButtons which populate a
hidden field client-side which could be checked server-side, but there
must surely be a better way than this?

Nov 18 '05 #2
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:Om**************@TK2MSFTNGP14.phx.gbl...
When you post back you have another event to set up the page, so you do
not have to put code in Page_Load. If you do, you are going contrary to
the MS event driven model. Unfortunately, almost every ASP.NET book out
there suggests code like this:
Yeah - looks like the code was lifted pretty much out of the WROX ASP.NET
1.0 samples...
Following this pattern, do the following:


Thanks very much.
Nov 18 '05 #3

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

Similar topics

3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
0
by: Steve | last post by:
I have a datagrid that is created at run time DataGrid dgG = new DataGrid(); BoundColumn bcB; dgG.CellPadding = 5; dgG.CellSpacing = 0; dgG.GridLines = GridLines.Both; dgG.CssClass =...
6
by: Erik H. | last post by:
Trying to connect to MySQL db on localhost, and populate datagrid from a dataset using code inline method. Getting the following compile error: Error Message: "CS0246: The type or namespace...
2
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for...
2
by: ddaniel | last post by:
I have read many posts and seen many papers on the different techniques for sort and filtering datagrids. Many do re-queries against the dB ala Fritz Onion. I am trying to leverage the Dataview....
5
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
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...
2
tjc0ol
by: tjc0ol | last post by:
Hi guys, Im just wondering that I cannot update my database throught datagrid datalist control, and the only thing works is that I can delete, cancel, edit but when I clicked update link...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.