2011-01-25 31 views

答えて

57

これはそれを行う必要があります。

私はここでそれを見つけた:http://forums.asp.net/p/1331581/2678206.aspx

<asp:TemplateField ShowHeader="False"> 
    <ItemTemplate> 
     <asp:ImageButton ID="DeleteButton" runat="server" ImageUrl="~/site/img/icons/cross.png" 
        CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?');" 
        AlternateText="Delete" />    
    </ItemTemplate> 
</asp:TemplateField> 
+0

イメージを使用したくない場合は、@ Saksham-Guptaの解決策のようにImageButtonの代わりにLinkBut​​tonを使用することもできます。これは –

4

あなたはボタンのOnClientClickを使用して行うことができます。

OnClientClick="return confirm('confirm delete')" 
+0

の下にありがとう、ポップアップメッセージボックス、メッセージボックスとしてではなく、Webページ自体に表示する方法はありますか? –

11

これは少し異なります。私のgridviewではAutoGenerateDeleteButton="true"を設定しました。削除ボタンを見つけるには、jQueryを使用して、見つかったアンカーにクリックイベントを追加します。

jQuery("a").filter(function() { 
     return this.innerHTML.indexOf("Delete") == 0; 
     }).click(function() { return confirm("Are you sure you want to delete this record?"); 
}); 

これはすばやく簡単です。 Deleteを表示するページのEvery AnchorがjQueryによって選択され、イベントが追加されることに注意してください。

+0

うまく動作します。より具体的なセレクターを追加して、必要に応じてグリッド内のアンカーを探すことで、さらに進化させることができます。 –

0

javascript confirm()関数を使用して取得する「Are you sure?」ポップアップでユーザーがキャンセルをクリックしたときのcommandField Deleteボタンの取得に問題がありました。私はそれをテンプレートフィールドに変更したくなかった。

問題は、私が見るように、これらのcommandFieldボタンには、ポストバックを実行するためのJavascriptがすでにいくつか関連付けられていることがあります。単にconfirm()関数を追加するだけの効果はありませんでした。ここで

が、私はそれを解決方法は次のとおりです。jQueryのを使用して

は、私が最初にそれぞれが(いくつかがある)ページ上のボタンを削除した場合、訪問者が合意されたかを確認するポップをキャンセルしたかどうかに基づいて、ボタンの関連するJavascriptを操作しますアップ。

<script language="javascript" type="text/javascript"> 
$(document).ready(function() { 

       $('input[type="button"]').each(function() { 

        if ($(this).val() == "Delete") { 
         var curEvent = $(this).attr('onclick'); 
         var newContent = "if(affirmDelete() == true){" + curEvent + "};" 
         $(this).attr('onclick',newContent);      
        } 
       }); 
} 

function affirmDelete() {  
       return confirm('Are you sure?'); 
} 
</script> 
1

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

私は更新に使用してボタンを削除します。それはボタンを編集しないでください。自動生成ボタンを使用することができます。

protected void gvOperators_OnRowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if(e.Row.RowType != DataControlRowType.DataRow) return; 
     var updateButton = (LinkButton)e.Row.Cells[0].Controls[0]; 
     if (updateButton.Text == "Update") 
     { 
      updateButton.OnClientClick = "return confirm('Do you really want to update?');"; 
     } 
     var deleteButton = (LinkButton)e.Row.Cells[0].Controls[2]; 
     if (deleteButton.Text == "Delete") 
     { 
      deleteButton.OnClientClick = "return confirm('Do you really want to delete?');"; 
     } 

    } 
19

あなたGridviewAutoGenerateDeleteButton="true"で使用する場合は、LinkButtonにそれを変換することができる:

  1. クリックしのGridViewタスク編集列https://www.flickr.com/photos/32784002@N02/15395933069/

  2. 選択選択したフィールドを削除し、このフィールドはTemplateFieldに変換をクリックしてください。そして、OKをクリックします。 https://www.flickr.com/photos/32784002@N02/15579904611/

  3. は、今すぐあなたのLinkButtonが生成されます。私はそれ他の列と一緒に簡単なエントリにするために@statmasterによって与えられた答えを変更して、私は任意の画像を望んでいなかった
    OnClientClick="return confirm('Are you sure you want to delete?'); "

4

:あなたはこのようLinkButtonOnClientClickイベントを追加することができます。

<asp:TemplateField ShowHeader="False"> 
     <ItemTemplate> 
      <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this entry?');">Delete </asp:LinkButton>    
     </ItemTemplate> 
</asp:TemplateField> 

Forecolorプロパティを使用してテキストの色を変更することができます。

+0

これはグリッドビューでの確認で削除する最も簡単な方法です – JoshYates1980

3

私はグリッドビューからレコードを削除する前に、確認のプロンプトを追加するこの方法が好きです。これは、aspxページのGridView Webコントロール内にネストされたCommandField定義です。ここには何もない - 単純なCommandfield。

<asp:CommandField ShowEditButton="true" UpdateText="Save" ShowDeleteButton="True"> 
 
    <ControlStyle CssClass="modMarketAdjust" /> 
 
</asp:CommandField>

その後、私がしなければならなかったすべては、GridViewコントロールのRowDeletingイベントにコードを追加しました。このイベントはの前に行が実際に削除されます。これにより、ユーザーの確認を取得し、後で取り消したくない場合にイベントをキャンセルすることができます。ここで私はRowDeletingイベントハンドラに入れたコードです:

Private Sub grdMarketAdjustment_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles grdMarketAdjustment.RowDeleting 
    Dim confirmed As Integer = MsgBox("Are you sure that you want to delete this market adjustment?", MsgBoxStyle.YesNo + MsgBoxStyle.MsgBoxSetForeground, "Confirm Delete") 
    If Not confirmed = MsgBoxResult.Yes Then 
    e.Cancel = True 'Cancel the delete. 
    End If 
End Sub 

はそれが正常に動作するようです。

+1

@ statmasterの回答はこの方が望ましいです。まず、コードをaspxページに置くだけで済みます(これにはaspxページとコードビハインドページの両方にコードが必要です)。第2に、このソリューションにはMsgBoxが必要であり、Webサーバーでは動作しない可能性があります。 MsgBoxを使用するのではなく、ScriptManagerを使用してコードビハインドでアラートを表示することができ、Visual StudioとWebサーバーの両方で動作します。しかし、コードビハインドにコードを必要としない単純なソリューションを使用するだけではどうですか? –

0

このコードは問題なく機能しています。

jQuery("a").filter(function() { 
     return this.innerHTML.indexOf("Delete") == 0; 
     }).click(function() { return confirm("Are you sure you want to delete this record?"); 
}); 
3

私はかなり彼らは削除しようとしている項目を正確にユーザーに通知するためにGridViewRowDataBoundイベントにコードを追加することなどです。やや良いユーザーエクスペリエンス

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     LinkButton lnkBtnDelete = e.Row.FindControl("lnkBtnDelete") as LinkButton; 

     // Use whatever control you want to show in the confirmation message 
     Label lblContactName = e.Row.FindControl("lblContactName") as Label; 

     lnkBtnDelete.Attributes.Add("onclick", string.Format("return confirm('Are you sure you want to delete the contact {0}?');", lblContactName.Text)); 

    } 

} 
0

私はJavaScriptのソリューションを愛し、動的なAjaxのロードで動作するようにいくつかの更新があります

 $(document).on("click", "a", function() { 
     if (this.innerHTML.indexOf("Delete") == 0) { 
      return confirm("Are you sure you want to delete this record?"); 
     } 
    }); 

はそれが助けを願って、あなたは内部に「削除の確認」オプションを追加したい場所

関連する問題