Page postback twice in ASP.NET 2.0 when ButtonType is set to Image in GridView

by Li Chen 3/29/2006 3:46:00 PM

This problem has driven me nuts for several hours before I found an answer.

I have GridView control that contains two command fields. When I clicked on the button, the page always got posted twice. After more testing, I found that this only occurs when the ButtonType is set to “Image”. The problem does not occur when the ButtonType is “Link” or “Button”.

On further Google search, I found the following:

This bug has been forwarded to Microsoft.
This is the answer from Microsoft Bug adn reported issues:

Thanks for reporting the issue. This is a known issue and we are
investigating fixing this in the next service pack. For the time being
you could use the following work around. One obvious workaround is to
change the button type to a regular button or a link button. If you need
an ImageButton, then you can put an ImageButton in a TemplateField. You
may need to handle the Command event on the ImageButton and call
DeleteRow, passing the RowIndex as the CommandArgument, like this:

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat=server id="ImageButton1" CommandName="Delete"
ImageUrl="..." CommandArgument='<%# DataBinder.Eval(Container,
"RowIndex") %>' OnCommand="ImageButton1_Command" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

protected void ImageButton1_Command(object sender, CommandEventArgs e) {
GridView1.DeleteRow(Int32.Parse(e.CommandArgument.ToString()));

}

Thanks,
The Web Platform and Tools Team

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.Net | ASP.NET

Comments

7/30/2006 10:26:00 PM

Li Chen

Test ClearScreen HIP Captcha control.

Li Chen us

1/27/2008 4:26:02 PM

Nick Chance

Hi Li - Really am grateful for this posting of yours I found on a Yahoo search. This problema has been giving me the proverbials all weekend. I used the workaround of the Image Button as a TemplateField and lo and behold: a working function. Many thanks for including the solution on this blog. I owe you my sanity. Nothing seemed to work. If it had been Webforms then I could've probably slugged the second trigger with some naff coding but with Webpages I had run out of "slugging techniques".
Once again cheers blue.

Nick Chance.
UK/Aus

Nick Chance gb

7/28/2008 4:35:56 PM

Miguel Martorell

Phewwwwwwwwwwwwwww! THis is one hell of a relieve. I have been hours searching for the answer and here it is. Hmm too bad I found out late, since many of my grids allready in the pages have image buttons. Good thing is this problem affected only one page where I would select the button and it would save. But with this problem, it would duplicate data. So Thank You! Now I can program in peace..

Miguel Martorell pr

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

1/5/2009 7:27:10 PM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  January 2009  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Pages

    Recent comments

    Authors

    Tags

      Disclaimer

      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      © Copyright 2009

      Sign in