2017-09-07 33 views
0

我想要处理按钮中按钮的按钮,然后从网站和数据库中删除该行的事件。我这样做,但在我的.aspx.cs文件中使用下面的代码:为什么我的rowcommand事件处理程序在这里不起作用?

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Delete") 
     { 
      System.Diagnostics.Debug.WriteLine("testing buttons"); 
     } 
    } 

但是,每当我去点击按钮区域内的按键,它抛出的错误,指出该事件没有处理。以下代码是整个数据表的aspx。

<asp:GridView ID="gridViewStudent" runat="server" CellPadding="4" ForeColor="#333333" 
     emptydatatext="There are no data to display"> 
     <AlternatingRowStyle BackColor="White" /> 
     <Columns> 
      <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Edit" ShowHeader="True" Text="Remove" /> 
     </Columns> 
     <EditRowStyle BackColor="#2461BF" /> 
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#EFF3FB" /> 
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
     <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
     <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
     <SortedDescendingHeaderStyle BackColor="#4870BE" /> 



</asp:GridView> 
+0

在codehind它的'GridView1_RowCommand '然后在aspx的GridView中调用'ID =“gridViewStudent”'?另外,它上面没有onrowcommand ='GridView1_RowCommand'。 –

+0

将您的事件名称更改为'gridViewStudent_RowCommand',并在控件上添加适当的事件处理程序:'OnRowCommand =“gridViewStudent_RowCommand”'。 –

+0

它表示OnRowCommand不是元素的有效属性ButtonField – Sohee

回答

0

从我所看到的,你不声明所有必需的事件处理程序的GridView。使用适当的事件处理程序和事件处理方法的相应动作:

页面标记(ASPX)

<asp:GridView ID="gridViewStudent" runat="server" ... 
     OnRowCommand="gridViewStudent_RowCommand" 
     OnRowDeleting="gridViewStudent_RowDeleting"> 

</asp:GridView> 

后面的代码(ASPX.CS)

// Row command event 
protected void gridViewStudent_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    // do something 
} 

// Row deleting event 
protected void gridViewStudent_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
    // do something 
} 
相关问题