suppressing Submit command with a Delete button

Discussion in 'ASP .Net' started by Elliot M. Rodriguez, Dec 16, 2003.

  1. I have a Delete button in my datagrid. I'm attempting to add some code
    confirming whether the user wishes to really delete the item or not.

    The confirm pops up, but I cannot suppress the submission when selecting
    Cancel.

    Heres what I have so far:

    ' adding the javascript call to the button:
    Private Sub dgItem_ItemCreated(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgItem.ItemCreated

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
    ListItemType.AlternatingItem Then

    Dim objDeleteButton As Button = CType(e.Item.Cells(1).Controls(0), Button)

    objDeleteButton.CssClass = "inputbutton"

    objDeleteButton.Attributes.Add("onClick", "confirmDeleteItem();")

    End If

    End Sub



    ' client side code in javascript

    function confirmDeleteItem()

    {

    if (confirm("Are you sure you wish to delete this item?"))

    {

    return true;

    }

    else

    {

    return false;

    }

    }

    any help is appreciated. thank you.
     
    Elliot M. Rodriguez, Dec 16, 2003
    #1
    1. Advertising

  2. Sounds like you have validation controls on there, which causes the runtime
    to emit yet another onClick handler which submits the form. Try using an
    <input type="button"> instead of an <asp:button> control, and if you need
    validation just script in the validation call (and, as always, make sure you
    explicitly validate on the server, as client side validation is easy to
    foil).

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    "Elliot M. Rodriguez" <elliotmrodriguezatnospamhotmail.com> wrote in message
    news:%23xSgcJ$...
    > I have a Delete button in my datagrid. I'm attempting to add some code
    > confirming whether the user wishes to really delete the item or not.
    >
    > The confirm pops up, but I cannot suppress the submission when selecting
    > Cancel.
    >
    > Heres what I have so far:
    >
    > ' adding the javascript call to the button:
    > Private Sub dgItem_ItemCreated(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles

    dgItem.ItemCreated
    >
    > If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
    > ListItemType.AlternatingItem Then
    >
    > Dim objDeleteButton As Button = CType(e.Item.Cells(1).Controls(0), Button)
    >
    > objDeleteButton.CssClass = "inputbutton"
    >
    > objDeleteButton.Attributes.Add("onClick", "confirmDeleteItem();")
    >
    > End If
    >
    > End Sub
    >
    >
    >
    > ' client side code in javascript
    >
    > function confirmDeleteItem()
    >
    > {
    >
    > if (confirm("Are you sure you wish to delete this item?"))
    >
    > {
    >
    > return true;
    >
    > }
    >
    > else
    >
    > {
    >
    > return false;
    >
    > }
    >
    > }
    >
    > any help is appreciated. thank you.
    >
    >
     
    Chris Jackson, Dec 16, 2003
    #2
    1. Advertising

  3. Chris:

    Thanks for the reply. I am struggling with this.

    If I take your approach it seems I dont have the ability to wire it to the
    DeleteCommand event. This means I have to access the DataKey for the row
    through the client, which I dont want to do. I could be wrong on this
    though.

    All I really want to do is have a DeleteCommand button that performs a
    delete with a confirmation prior to deletion. My code does not have any
    other validators on the page. There is another button on the page, but
    performs a completely unrelated task.


    "Chris Jackson" <chrisjATmvpsDOTorgNOSPAM> wrote in message
    news:uV5h$Y$...
    > Sounds like you have validation controls on there, which causes the

    runtime
    > to emit yet another onClick handler which submits the form. Try using an
    > <input type="button"> instead of an <asp:button> control, and if you need
    > validation just script in the validation call (and, as always, make sure

    you
    > explicitly validate on the server, as client side validation is easy to
    > foil).
    >
    > --
    > Chris Jackson
    > Software Engineer
    > Microsoft MVP - Windows Client
    > Windows XP Associate Expert
    > --
    > More people read the newsgroups than read my email.
    > Reply to the newsgroup for a faster response.
    > (Control-G using Outlook Express)
    > --
    >
    > "Elliot M. Rodriguez" <elliotmrodriguezatnospamhotmail.com> wrote in

    message
    > news:%23xSgcJ$...
    > > I have a Delete button in my datagrid. I'm attempting to add some code
    > > confirming whether the user wishes to really delete the item or not.
    > >
    > > The confirm pops up, but I cannot suppress the submission when selecting
    > > Cancel.
    > >
    > > Heres what I have so far:
    > >
    > > ' adding the javascript call to the button:
    > > Private Sub dgItem_ItemCreated(ByVal sender As Object, ByVal e As
    > > System.Web.UI.WebControls.DataGridItemEventArgs) Handles

    > dgItem.ItemCreated
    > >
    > > If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
    > > ListItemType.AlternatingItem Then
    > >
    > > Dim objDeleteButton As Button = CType(e.Item.Cells(1).Controls(0),

    Button)
    > >
    > > objDeleteButton.CssClass = "inputbutton"
    > >
    > > objDeleteButton.Attributes.Add("onClick", "confirmDeleteItem();")
    > >
    > > End If
    > >
    > > End Sub
    > >
    > >
    > >
    > > ' client side code in javascript
    > >
    > > function confirmDeleteItem()
    > >
    > > {
    > >
    > > if (confirm("Are you sure you wish to delete this item?"))
    > >
    > > {
    > >
    > > return true;
    > >
    > > }
    > >
    > > else
    > >
    > > {
    > >
    > > return false;
    > >
    > > }
    > >
    > > }
    > >
    > > any help is appreciated. thank you.
    > >
    > >

    >
    >
     
    Elliot M. Rodriguez, Dec 16, 2003
    #3
  4. Aha - that's a special case I hadn't considered.

    Try this article:

    http://authors.aspalliance.com/aldotnet/examples/cd.aspx


    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    "Elliot M. Rodriguez" <someemail> wrote in message
    news:...
    > Chris:
    >
    > Thanks for the reply. I am struggling with this.
    >
    > If I take your approach it seems I dont have the ability to wire it to the
    > DeleteCommand event. This means I have to access the DataKey for the row
    > through the client, which I dont want to do. I could be wrong on this
    > though.
    >
    > All I really want to do is have a DeleteCommand button that performs a
    > delete with a confirmation prior to deletion. My code does not have any
    > other validators on the page. There is another button on the page, but
    > performs a completely unrelated task.
    >
    >
    > "Chris Jackson" <chrisjATmvpsDOTorgNOSPAM> wrote in message
    > news:uV5h$Y$...
    > > Sounds like you have validation controls on there, which causes the

    > runtime
    > > to emit yet another onClick handler which submits the form. Try using an
    > > <input type="button"> instead of an <asp:button> control, and if you

    need
    > > validation just script in the validation call (and, as always, make sure

    > you
    > > explicitly validate on the server, as client side validation is easy to
    > > foil).
    > >
    > > --
    > > Chris Jackson
    > > Software Engineer
    > > Microsoft MVP - Windows Client
    > > Windows XP Associate Expert
    > > --
    > > More people read the newsgroups than read my email.
    > > Reply to the newsgroup for a faster response.
    > > (Control-G using Outlook Express)
    > > --
    > >
    > > "Elliot M. Rodriguez" <elliotmrodriguezatnospamhotmail.com> wrote in

    > message
    > > news:%23xSgcJ$...
    > > > I have a Delete button in my datagrid. I'm attempting to add some code
    > > > confirming whether the user wishes to really delete the item or not.
    > > >
    > > > The confirm pops up, but I cannot suppress the submission when

    selecting
    > > > Cancel.
    > > >
    > > > Heres what I have so far:
    > > >
    > > > ' adding the javascript call to the button:
    > > > Private Sub dgItem_ItemCreated(ByVal sender As Object, ByVal e As
    > > > System.Web.UI.WebControls.DataGridItemEventArgs) Handles

    > > dgItem.ItemCreated
    > > >
    > > > If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
    > > > ListItemType.AlternatingItem Then
    > > >
    > > > Dim objDeleteButton As Button = CType(e.Item.Cells(1).Controls(0),

    > Button)
    > > >
    > > > objDeleteButton.CssClass = "inputbutton"
    > > >
    > > > objDeleteButton.Attributes.Add("onClick", "confirmDeleteItem();")
    > > >
    > > > End If
    > > >
    > > > End Sub
    > > >
    > > >
    > > >
    > > > ' client side code in javascript
    > > >
    > > > function confirmDeleteItem()
    > > >
    > > > {
    > > >
    > > > if (confirm("Are you sure you wish to delete this item?"))
    > > >
    > > > {
    > > >
    > > > return true;
    > > >
    > > > }
    > > >
    > > > else
    > > >
    > > > {
    > > >
    > > > return false;
    > > >
    > > > }
    > > >
    > > > }
    > > >
    > > > any help is appreciated. thank you.
    > > >
    > > >

    > >
    > >

    >
    >
     
    Chris Jackson, Dec 16, 2003
    #4
  5. Chris:

    I realized I forgot the lousy "return" keyword in Attributes.Add lol...
    Thanks for the article and the help.

    "Chris Jackson" <chrisjATmvpsDOTorgNOSPAM> wrote in message
    news:...
    > Aha - that's a special case I hadn't considered.
    >
    > Try this article:
    >
    > http://authors.aspalliance.com/aldotnet/examples/cd.aspx
    >
    >
    > --
    > Chris Jackson
    > Software Engineer
    > Microsoft MVP - Windows Client
    > Windows XP Associate Expert
    > --
    > More people read the newsgroups than read my email.
    > Reply to the newsgroup for a faster response.
    > (Control-G using Outlook Express)
    > --
    >
    > "Elliot M. Rodriguez" <someemail> wrote in message
    > news:...
    > > Chris:
    > >
    > > Thanks for the reply. I am struggling with this.
    > >
    > > If I take your approach it seems I dont have the ability to wire it to

    the
    > > DeleteCommand event. This means I have to access the DataKey for the row
    > > through the client, which I dont want to do. I could be wrong on this
    > > though.
    > >
    > > All I really want to do is have a DeleteCommand button that performs a
    > > delete with a confirmation prior to deletion. My code does not have any
    > > other validators on the page. There is another button on the page, but
    > > performs a completely unrelated task.
    > >
    > >
    > > "Chris Jackson" <chrisjATmvpsDOTorgNOSPAM> wrote in message
    > > news:uV5h$Y$...
    > > > Sounds like you have validation controls on there, which causes the

    > > runtime
    > > > to emit yet another onClick handler which submits the form. Try using

    an
    > > > <input type="button"> instead of an <asp:button> control, and if you

    > need
    > > > validation just script in the validation call (and, as always, make

    sure
    > > you
    > > > explicitly validate on the server, as client side validation is easy

    to
    > > > foil).
    > > >
    > > > --
    > > > Chris Jackson
    > > > Software Engineer
    > > > Microsoft MVP - Windows Client
    > > > Windows XP Associate Expert
    > > > --
    > > > More people read the newsgroups than read my email.
    > > > Reply to the newsgroup for a faster response.
    > > > (Control-G using Outlook Express)
    > > > --
    > > >
    > > > "Elliot M. Rodriguez" <elliotmrodriguezatnospamhotmail.com> wrote in

    > > message
    > > > news:%23xSgcJ$...
    > > > > I have a Delete button in my datagrid. I'm attempting to add some

    code
    > > > > confirming whether the user wishes to really delete the item or not.
    > > > >
    > > > > The confirm pops up, but I cannot suppress the submission when

    > selecting
    > > > > Cancel.
    > > > >
    > > > > Heres what I have so far:
    > > > >
    > > > > ' adding the javascript call to the button:
    > > > > Private Sub dgItem_ItemCreated(ByVal sender As Object, ByVal e As
    > > > > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > > > dgItem.ItemCreated
    > > > >
    > > > > If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
    > > > > ListItemType.AlternatingItem Then
    > > > >
    > > > > Dim objDeleteButton As Button = CType(e.Item.Cells(1).Controls(0),

    > > Button)
    > > > >
    > > > > objDeleteButton.CssClass = "inputbutton"
    > > > >
    > > > > objDeleteButton.Attributes.Add("onClick", "confirmDeleteItem();")
    > > > >
    > > > > End If
    > > > >
    > > > > End Sub
    > > > >
    > > > >
    > > > >
    > > > > ' client side code in javascript
    > > > >
    > > > > function confirmDeleteItem()
    > > > >
    > > > > {
    > > > >
    > > > > if (confirm("Are you sure you wish to delete this item?"))
    > > > >
    > > > > {
    > > > >
    > > > > return true;
    > > > >
    > > > > }
    > > > >
    > > > > else
    > > > >
    > > > > {
    > > > >
    > > > > return false;
    > > > >
    > > > > }
    > > > >
    > > > > }
    > > > >
    > > > > any help is appreciated. thank you.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Elliot M. Rodriguez, Dec 17, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    1,767
  2. Ghafran Abbas
    Replies:
    0
    Views:
    4,281
    Ghafran Abbas
    Oct 12, 2004
  3. Replies:
    2
    Views:
    6,324
  4. Network-Man
    Replies:
    5
    Views:
    79,199
    gaul1
    Jul 7, 2012
  5. Guest
    Replies:
    2
    Views:
    1,644
    Guest
    Oct 25, 2004
Loading...

Share This Page