By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Datagrid & Page_Load

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.