2017-09-07 5 views
0

버튼 필드의 버튼을 누른 다음 웹 사이트와 데이터베이스에서 행을 제거하는 이벤트를 처리하려고합니다. 나는 그렇게하지만 내 .aspx.cs 파일에 다음 코드를 사용하여 : I 버튼 필드에서 버튼을 클릭하여 이동 할 때마다왜 내 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 
}