GridView中的ButtonType="Image"会导致回发两次

本文介绍了ASP.NET 2.0中使用GridView控件时,将按钮类型设置为图像(Image)导致页面双次回发的问题。此现象仅在按钮类型为图像时出现,在链接(Link)或普通按钮(Button)情况下则不会发生。文章提供了来自微软官方的解决方案,包括将按钮更改为链接或普通按钮,或者在模板字段中使用ImageButton并处理Command事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值