質問

Asp.netアプリケーションに GridView コントロールがあり、< asp:buttonField> type =" image" および CommandName =" Delete"

OnRowDelete イベントに到達する前にjavascriptを実行する方法はありますか?

行を削除する前に簡単な確認が必要です。

ありがとう!

編集< asp:ButtonField> タグには OnClientClick 属性がないことに注意してください。

役に立ちましたか?

解決

代わりにTemplateFieldを使用し、必要なものに応じて、通常のasp:Buttonまたはasp:ImageButtonをItemTemplateに設定します。その後、DeleteコマンドをインターセプトしたときにRowCommandイベントが実行するのと同じロジックを実行できます。

これらのボタンのいずれかで、OnClientClickプロパティを使用して、この前にJavaScript確認ダイアログを実行します。

<script type="text/javascript">
   function confirmDelete()
   {
       return confirm("Are you sure you want to delete this?");
   }
</script>

...

<asp:TemplateField>
  <ItemTemplate>
     <asp:ImageButton ID="DeleteButton" runat="server"
        ImageUrl="..." AlternateText="Delete" ToolTip="Delete"
        CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
        OnClientClick="return confirmDelete();" />
  </ItemTemplate>
</asp:TemplateField>

他のヒント

これを行う最もエレガントな方法は、jQueryを使用してonClickイベントをワイヤリングすることであることがわかりました。

<script type="text/javascript"> 
    $(".deleteLink").click(function() {
      return confirm('Are you sure you wish to delete this record?');
    });
</script>

...

<asp:ButtonField ButtonType="Link" Text="Delete"
    CommandName="Delete" ItemStyle-CssClass="deleteLink" />

リンクボタンを識別するために任意のCSSクラスを使用していることに注意してください。

GridView RowCreated イベントハンドラーで、 FindControl を使用して名前付きボタンを見つけ、Attributesコレクションに追加します。

btn.Attributes.Add("onclick", "return confirm('delete this record?');");

ASP.Netコードは、confirm()がtrueの場合、つまりOKされた場合にのみ実行されます。

だから私はjavascript関数を持っています:

function confirmDeleteContact() {
  if (confirm("Are you sure you want to delete this contact?")) {
    document.all.answer.value="yes";
  } else {
    document.all.answer.value="no";
  }
}

そして次のようにグリッド項目に配線します:

Sub dgbind(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgcontacts.ItemDataBound
    Select Case e.Item.ItemType
        Case ListItemType.Item, ListItemType.AlternatingItem
            CType(e.Item.Cells(9).Controls(0), System.Web.UI.WebControls.LinkButton).Attributes.Add("onclick", "javascript:confirmDeleteContact();")
    End Select
End Sub

これは古いコードなので、変更できるものがいくつかありますが、モラルは次のとおりです。他のすべてが失敗した場合は、javascript&quot; onClick&quot;行バインディング中。 &quot; document.all.answer.value&quot; runat = server を持つ非表示フィールドです。これにより、ポストバック時に値を読み取ることができます。

buttonfieldを使用する場合、追加参照System.Windows.Formsの方が良い...すべての.netフレームワークで常に利用可能であり、asp.net ..をサポートしています。

ボタンフィールドが最良の選択である場合、これはあなたの選択です。 サンプル:

using System.Windows.Forms;

protected void BorrowItem_RowCommand(object sender, GridViewCommandEventArgs e)
{

    if (e.CommandName == "Delete")
    {

        if (System.Windows.Forms.MessageBox.Show("Do you want to delete", "Delete",MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification) != System.Windows.Forms.DialogResult.OK)
        {
            return;
         }
     }
//Continue execution...
}

//drimaster
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top