GridView的RowCommand事件不触发
问题描述:
我有一个GridView看起来是这样的:
I have a GridView that looks something like this:
<asp:GridView
ID="GridView1"
AllowPaging="true"
OnRowCommand="RowCommand"
OnPageIndexChanging="gridView_PageIndexChanging"
Runat="server">
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" ButtonType="Button" CommandName="ItemExport" CommandArgument='<%# Eval("EXPORT") %>'
Text="Export" runat="server" />
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
下面是 RowCommand
:
protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ItemExport")
{
// etc.
}
}
单击该按钮在不触发的 RowCommand
事件都没有。然而, RowCommand
火灾时,我点击GridView的寻呼机页索引。
Clicking the button is not firing the RowCommand
event at all. However, RowCommand
fires when I click a page index in the GridView's pager.
答
您必须的不可以绑定在Page_Load中回传网格,只有当一些改变,导致电网重新加载数据(FE排序,寻呼),并且仅在适当的事件处理程序。
You must not bind your grid on postbacks in Page_Load, only when something changed that causes the Grid to reload data(f.e. Sorting,Paging) and only in the appropriate event-handlers.
另一个可能的原因:是否禁用ViewState的地方
Another possible reason: Have you disabled ViewState somewhere?