停灯向晓  

/* from: http://www.dotneteer.com/Weblog/post/2006/03/Page-postback-twice-in-ASPNET-20-when-ButtonType-is-set-to-Image-in-GridView.aspx */

 

以下为全文引用,我没有验证:

 

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

 

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

posted on 2009-12-25 15:59  kofkyo  阅读(186)  评论(0编辑  收藏  举报