WebControls.DataGrid Sort command event fires twice

Discussion in 'ASP .Net Web Controls' started by Haile, Dec 5, 2003.

  1. Haile

    Haile Guest

    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, Dec 5, 2003
    1. Advertisements

  2. Haile

    Haile Guest

    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, Dec 5, 2003
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Barrows

    Re: Page Load Event Fires Twice

    Bob Barrows, Jul 9, 2003, in forum: ASP .Net
    Bob Barrows
    Jul 9, 2003
  2. Steve C. Orr, MCSD

    Re: Page Load Event Fires Twice

    Steve C. Orr, MCSD, Jul 9, 2003, in forum: ASP .Net
    Wray Smallwood
    Jul 10, 2003
  3. Jonas Nilsson

    ItemCommand Event fires twice

    Jonas Nilsson, Aug 1, 2003, in forum: ASP .Net
    Jonas Nilsson
    Aug 1, 2003
  4. Jon
    George Ter-Saakov
    Jan 13, 2004
  5. Replies:

Share This Page