WebControls.DataGrid Sort command event fires twice

H

Haile

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)
e.SortExpression;
}

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.

Best,
Haile
 
H

Haile

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,
Haile
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top