2017-09-07 2 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

:適切なイベントハンドラと、それぞれのアクションのためのイベントハンドラメソッドを使用します'次に、aspxでGridViewは' ID = "gridViewStudent" 'と呼ばれていましたか?プラスonrowcommand = 'GridView1_RowCommand'はありません。 –

+0

イベント名を 'gridViewStudent_RowCommand'に変更し、適切なイベントハンドラをコントロールに追加します:' OnRowCommand = "gridViewStudent_RowCommand" '。 –

+0

OnRowCommandはButtonField要素の有効な属性ではないと言います – Sohee

答えて

0

私の知るところでは、必要なイベントハンドラをすべてGridViewに宣言しているわけではありません。

ページのマークアップ(ASPX)それは `GridView1_RowCommandだcodehindで

<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 
} 
関連する問題