GridView Command Button Causing Two Successive Postbacks

Last post 10-28-2008 5:34 AM by Qin Dian Tang - MSFT. 3 replies.

Sort Posts:

  • GridView Command Button Causing Two Successive Postbacks

    10-23-2008, 4:46 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 876

    I posted a variation of this on an AJAX forum but think I was perhaps barking up the wrong tree, so let me try again here:

    Here's an abridged glimpse of a GridView definition that I have:

      <asp:GridView ID="gridViewReports" runat="server" OnRowCommand="gridViewReports_RowCommand">

        <asp:ButtonField ImageUrl="~/Images/Buttons/up.png" ButtonType="Image" CommandName="Up" ItemStyle-CssClass="clickable" />
        <asp:ButtonField ImageUrl="~/Images/Buttons/down.png" ButtonType="Image" CommandName="Down" ItemStyle-CssClass="clickable" />

      </asp:GridView>

     

    Stepping through the code, Page_Load is fired twice whenever I click on one of these command buttons.  The first time through, IsPostBack is always set to false (which is of course erroneous except for the very first time the form is loaded).

    And strangely enough ... each row also has a checkbox in it.  Clicking on the checkbox does cause a postback, but only one as per normal. 

    Why would the buttons be causing two postbacks?

    Robert
    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: GridView Command Button Causing Two Successive Postbacks

    10-23-2008, 10:22 PM
    • Contributor
      4,722 point Contributor
    • ameenkpn
    • Member since 05-21-2007, 5:07 AM
    • Posts 806

     If you have set the AutoPostBack property of the CheckBox to True, then for sure again RowCommand property will postback. Also every time the field with CommandName is clicked, it will call the GridView's RowCommand event. In the RowCommand event, you have to check the CommandName and do the codings you want.

    if (e.CommandName.Equals("Up"))
    {
      //Do things when Up button is clicked.
    }

    if (e.CommandName.Equals("Down"))
    {
      //Do things when Down button is clicked.
    }

     


     

    Marking a Reply as 'Answered', not only GAIN us some POINTS, but it also HELP others to find RIGHT solution.
  • Re: GridView Command Button Causing Two Successive Postbacks

    10-23-2008, 10:50 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 876

    Please be aware that the Checkbox column is a separate one and is NOT causing two postbacks.  It's only the CommandName fields that are causing two postbacks.

    Robert

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: GridView Command Button Causing Two Successive Postbacks

    10-28-2008, 5:34 AM

    Hi rmdw,

    Many people have this issue with ImageButton in GridView. I cannot say it is a bug, but you can try to report it to

    http://connect.microsoft.com/VisualStudio/

    Thanks,
    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (4 items)