关于Repeater中绑定的控件不触发ItemCommand事件

时间:2023-03-09 23:51:50
关于Repeater中绑定的控件不触发ItemCommand事件

今天遇到 在repeater 中使用一个button,点击button然后跳转另外一个页面。

html.

 <asp:Repeater ID="repeater" runat="server" OnItemCommand="repeater_ItemCommand">
<ItemTemplate>
<table>
<tr>
<td>id:<%# Eval("id") %></td>
<td>
<%--<asp:ImageButton id="bnt12" ImageUrl="~/help.png" runat="server" CommandName="跳转"/>--%>
<asp:Button runat="server" Text="跳转" ID="bnt1" CommandName="跳转" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>

cs.

      protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "跳转")
{
Response.Redirect("http://www.baidu.com");
}
}

当点击button的时候,发现是不会跳转的,

在网上也一直找了很多资料,最后发现可能的原因是

  1.当点击button的时候 是刷新页面 repeater 重新绑定了一遍, 把itemcommand 刷新没了,

  2.使用dopostback 类型的都可以跳页面,既linkbutton.

  解决方法:点击button的时候 不让repeater重新绑定数据 or 使用dopostback。