Skip to content Skip to sidebar Skip to footer

Javascript Before Asp:buttonfield Click

I have a GridView control in an Asp.net application, that has a of type='image' and CommandName='Delete'. Is there any way to execute a piece of javascript

Solution 1:

I would use a TemplateField instead, and populate the ItemTemplate with a regular asp:Button or asp:ImageButton, depending one what is needed. You can then execute the same logic that the RowCommand event was going to do when it intercepted the Delete command.

On either of those buttons I would then use the OnClientClick property to execute the JavaScript confirm dialog prior to this.

<scripttype="text/javascript">functionconfirmDelete()
   {
       returnconfirm("Are you sure you want to delete this?");
   }
</script>

...

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

Solution 2:

I found that the most elegant way to do this is to use jQuery to wire the onClick event:

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

...

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

Notice that I use an arbitrary CSS class to identify the link button.

Solution 3:

In the GridView's RowCreated event handler, use FindControl to find the named button, and add to the Attributes collection:

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

Your ASP.Net code will only be executed if confirm() is true, i.e. has been ok'd.

Solution 4:

So I have a javascript function:

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

and I wire it to a grid item like so:

Sub dgbind(ByVal sender AsObject, ByVal e As DataGridItemEventArgs) Handles dgcontacts.ItemDataBound
    SelectCase 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();")
    EndSelectEndSub

This is some old code, so I see a few things I could change up, but the moral is this: If all else fails, add the javascript "onClick" during row binding. "document.all.answer.value" is a hidden field that has runat=server so that I can read the value upon postback.

Solution 5:

better for you the add reference System.Windows.Forms if you use buttonfield... It is always available in all .net framework and supports asp.net..

this is your choice if the buttonfield is your best choice.. sample:

using System.Windows.Forms;

protectedvoidBorrowItem_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

Post a Comment for "Javascript Before Asp:buttonfield Click"