Velocity Reviews

Velocity Reviews (
-   ASP .Net Web Controls (
-   -   WebControls.DataGrid Sort command event fires twice (

Haile 12-05-2003 06:15 PM

WebControls.DataGrid Sort command event fires twice
I have a DataGrid with AllowSorting set to true and
several sort expressions defined for bound columns. In
the event handler a Response.Write() command reports that
the event has fired twice on every single click to the
sort link.

ASPX Code:

<asp:datagrid id="dg1" onsortcommand="dg1SortCommand" ...
<asp:boundcolumn ... sortexpression="lastName ASC" ...

CodeBehind (cs):

protected void dg1SortCommand( .. source, .. e )
// update DataView
// dg1.DataBind()
Response.Write("clicked sort column:"+(String)

When this is run, one click causes the event handler to
write the "clicked..." message twice. What is causing it
to fire the second time?

I would appreciate any assistance.


Haile 12-05-2003 08:14 PM

WebControls.DataGrid Sort command event fires twice
Answered my own question. Heads up everyone, because your
code could be taking two round-trips to the DBMS if you
are not watching for it.

Most of the documentation I have seen recommends putting
an "OnSortCommand" property in the ASPX datagrid tag (for
example, Esposito, "Building Web Solutions," Microsoft
Press, pg 59). The problem is that the DataGrid property
name is "SortCommand," which one must also set when
working in the .NET IDE, or else the event doesn't get
declared in the IDE's form designer region. And here's
the rub - the IDE isn't smart enough to equate the two
properties - it happily creates a Javascript postback call
for each of the two event declarations. This is
inconsistent with the behavior of just about every other
aspect of the DataGrid control. For example, if in the
ASPX page you set the DataGrid's cssClass property
to "someValue," this setting finds its way to the
control's property page.

To solve the double-firing problem, I took
the "OnSortCommand=" expression out of the ASPX file.
Curiously, if you try to enter "SortCommand=.." in the
ASPX file, the IDE reports an HTML schema error, so I have
not found a way to make the HTML match up with the
settings in the property page.

Finally, one should also remove "onPageIndexChanged" from
the ASPX code as well, as this causes another double-fire
if paging is enabled. This one can be disastrous
performance-wise if your grid uses custom paging.

Pardon me if this sounds like a rant. On the whole I am
impressed by .NET's consistency.

Best regards,

All times are GMT. The time now is 09:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.