2011-08-15 9 views
1

gridviewのデフォルトの削除コマンドフィールドを使用しているgridviewから行を削除しています。クリックすると、グリッドビューの行削除コマンドが実行され、選択した行が削除されます。今までのすべての良い。gridviewの行削除イベントで削除を確認するためのjavascriptを書く

しかし、行が削除される前に、私はユーザーに確認メッセージを設定する必要があります。 OKボタンをクリックすると、行が削除されます(キャンセルボタンをクリックすると)。

私は以下のコードを持っています。

return confirm('Are you sure to delete?'); 

しかし、私は簡単にLinkBut​​tonコントロールのOnClickイベントに書くことができますし、GridViewのRowDataBoundイベントに属性を追加することができるよう(代わりに、コマンドフィールドの)LinkBut​​tonコントロールがある場合、これは正常に動作します。

コマンドのフィールド削除ボタンでも同じことができますか?ご案内ください!

ありがとうございます!ここで

+0

あなたはjqueryが好きですか、またはjavascriptで何かをお探しですか? – naveen

+0

何でもいいです。 –

答えて

2

この記事はあなたが必要なものを正確に行う方法を説明しますのでご注意ください:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // loop all data rows 
     foreach (DataControlFieldCell cell in e.Row.Cells) 
     { 
      // check all cells in one row 
      foreach (Control control in cell.Controls) 
      { 
       // Must use LinkButton here instead of ImageButton 
       // if you are having Links (not images) as the command button. 
       ImageButton button = control as ImageButton; 

       if (button != null && button.CommandName == "Delete") 
        // Add delete confirmation 
        button.OnClientClick = "return confirm('Are you sure you want to delete this record?');"; 
      } 
     } 
    } 
} 
+0

これは受け入れられる答えですが、動作しません。確認ダイアログが正しく表示されますが、[OK]をクリックするとポストバックイベントは生成されません。あなたのコードはそのcodeprojectページのコードとは異なります。コードを更新してください。 –

+0

コードの実装に問題があると思われましたか?かなりストレートなものがここにあります。 –

+0

何かが間違っています、もちろん私のコードで間違っています。しかし、私は本当にその "返品確認( '...');を書き直す必要があります"if(!confirm( '...'))falseを返します。私のFirefoxで動作するように。効果的には「真実なら返さない」という意味です。他の変種は私のために働かない。あなたは何が間違っているのか分かりますか? –

2

は、使用できるコードです....

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     foreach (DataControlField dcf in GridView1.Columns) 
     { 

      if (dcf.ToString() == "CommandField") 
      { 
       if (((CommandField)dcf).ShowDeleteButton == true) 
       { 
        e.Row.Cells[GridView1.Columns.IndexOf(dcf)].Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');"); 
       } 
      } 
     } 
    } 
} 
+0

いいえ、リンクボタンやテンプレートフィールドは使用できません。私はコマンドフィールド削除ボタンを使用する必要があります –

+0

私の答えを更新しました。 –

+0

rowdeletingイベントでグリッドビュー行を削除するコードを記述していれば、コードは機能しますか? –

0

これを試してみてください。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     foreach (DataControlFieldCell cell in e.Row.Cells) 
     { 
      foreach (Control control in cell.Controls) 
      { 
       // Choose between Button, ImageButton and LinkButton. 
       // as my ButtonType="Button". 
       Button button = control as Button; 
       if (button != null && button.CommandName == "Delete") 
       { 
        string script = "if(confirm('Are you sure to delete?')) __doPostBack('{0}','{1}${2}'); else return false;"; 
        string clickEvent = String.Format(
         script, 
         GridView1.ClientID, 
         button.CommandName, 
         button.CommandArgument); 
        button.Attributes.Add("onclick", clickEvent);         
        break; 
       } 
      } 
     } 
    } 
} 

私が予想していたよりもずっと汚いです。

http://www.codeproject.com/KB/webforms/Gridview_Delete_confirmLS.aspx

をそして、ここであなたがそれを行うために必要なコードです:asp:TemplateField :)

を使用することをお勧め私のButtonType="Button"

+0

Gridview_rowdeletingイベントで行を削除するコードを記述していればコードはありますか? –

+0

はい。私はそれをテストしました – naveen

関連する問題