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