Datagrid & Page_Load

M

Mark Rae

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?
 
C

Cowboy \(Gregory A. Beamer\)

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!
*************************************************
 
M

Mark Rae

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top