Can't use datagrid pager with javascript onbeforeupdate event

J

James

Hi

The standard datagrid pager is rendered as a hyperlink e.g. <a
href="javascript:__doPostBack('...', '...')">

Apparently use of this javascript pseudo-protocol is bad practice, and
it was introduced to allow people to generate pages using javascript.
So of course it is all over ASP.Net and particularly the datagrid
pager.

One bad thing about this is that despite apparently not navigating to a
new page, because of the intention that the javascript would generate a
new page, this link when activated immediately fires the onbeforeupdate
event for the page. I have code in this event which I only want to run
when the page is being left for a new page, not when the page is merely
being posted back. I can find no way to use the ASP.Net datagrid
control and do this, particularly I can't tell that someone has just
clicked the pager on this control and is therefore not navigating to a
new page.

I thought there might be some means of determining the cause of the
onbeforeupdate event being fired but the event object does not support
srcElement or target in this case. I have tried 'hijacking' the
__doPostBack event by having some javascript which runs after the
declaration of __doPostBack rendered by the ASP.Net framework and
redefines __doPostBack to point to another function. I had hoped by
setting a flag in this function, I could tell that a post back was
occurring not a navigation to a new page. However because of this
hyperlink based invocation of __doPostBack, the onbeforeupdate event
fires before this code can set the flag.

I have tried to write an inheritor of DataGrid which messes with the
rendering of the pager, however although I can get at the LinkButton
child controls that the DataGrid creates for the pager, I can't get at
the href they render, and so I can't get the parameters that are passed
to __doPostBack nor can I generate them with any method on the Page
object.

Does anyone know of a workaround for this?

James
 
J

James

Well I've managed to answer this myself. Its a hack but it works.
Essentially you dig into the object model of the DataGrid in the
OnItemCreated event and set onclick events on the hyperlinks.
Fortunately these onclick events fire before the onbeforeupdate is
triggered. You set a flag in the onclick event which tells your
onbeforeupdate event that its been triggered by a postback. The code
below sets these onclick events on header sorting hyperlinks and on
footer paging hyperlinks.

protected override void OnItemCreated(DataGridItemEventArgs e)
{
base.OnItemCreated(e);

// We need to ensure controls on the datagrid which cause postback set
the InPostBack flag
// to true before they do this.
if (e.Item.ItemType == ListItemType.Pager)
{
Control ctl = e.Item.Controls[0];
LinkButton btn;
if ((btn = ctl.Controls[0] as LinkButton) != null)
{
btn.Attributes.Add("onclick", "InPostBack = true;");
}
if ((btn = ctl.Controls[2] as LinkButton) != null)
{
btn.Attributes.Add("onclick", "InPostBack = true;");
}
}
else if (e.Item.ItemType == ListItemType.Header)
{
TableCell cell;
LinkButton btn;
foreach (Control ctl in e.Item.Controls)
if ((cell = ctl as TableCell) != null)
if ((btn = cell.Controls[0] as LinkButton) != null)
btn.Attributes.Add("onclick", "InPostBack = true;");
}
}

Hope this helps some people

James
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top