2011-03-17 4 views
0

항목 템플릿을 통해 제거 버튼을 추가하는 gridview를 만들었습니다. 나는 그가 클릭하는 버튼에 따라 동적으로 그리드 뷰에서 행을 제거하는 방법을 알고 싶습니다. 런타임에 gridview에서 동적으로 행을 제거합니다.

protected void RemoveBtn_OnClick(object sender, EventArgs e) 
    { 
     Button clickedButton = sender as Button; 
     GridViewRow row = (GridViewRow)clickedButton.Parent.Parent; 
     int rowID = Convert.ToInt16(row.RowIndex); 

     GridView1.DeleteRow(rowID); 
    } 



<asp:GridView ID= "GridView1" runat="server" AutoGenerateColumns="true" OnRowDeleting="RowDeletingEvent"> 
<Columns> 
    <asp:TemplateField HeaderText="Remove Items"> 
     <ItemTemplate> 
      <asp:Button id="RemoveBtn" runat="server" Text="Remove" OnClick="RemoveBtn_OnClick"/> 
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 
</asp:GridView> 

는 기대

답변

2

사용 GridView.RowCommand Event에 감사드립니다. 버튼이 의 GridView 컨트롤 클릭하면

RowCommand 이벤트가 발생된다. 이렇게하면 이벤트가 발생할 때마다 사용자 지정 루틴을 수행하는 이벤트 처리 방법을 에게 제공 할 수 있습니다.

도 컨트롤의 기본 제공 기능을 호출 할 수 있습니다. 이러한 작업 중 하나를 수행하려면 단추의 CommandName 속성을 다음 테이블의 값 중 하나로 설정하십시오.

CommandName Value 
"Delete" - Deletes the current record. Raises the RowDeleting and RowDeleted events. 
관련 문제