views:

518

answers:

4

I have a table that is created in a DataList in ASP.Net. This table has three fields of text, then a field with an edit button, and a field with a delete button. When a person clicks the delete button, it posts back, deletes the items, and then binds the DataList again. The DataList is in an UpdatePanel so the item smoothly disappears after a half of a second or maybe a little more, but what I'd really like is for the row to slide out (up) as soon as they hit the delete button, and then have it delete the item on the post back.

I can make the row slide out with jQuery, but the postback gets in the way. How do you deal with that?

A: 

have you tried looking at any of the Ajax control toolkit items? I believe there are some controls in there that will head with client side (java) code if your not extremely familiar

Miles
A: 

I would use client side Javascript to manually scale the "opacity" CSS property down to zero, then mark the element as "display: none" then submit the post-back.

I believe that in Internet Explorer, you need to use the "Filter" CSS property to do this as it does not support opacity.

You could just code up a routine to set both properties and that should cover all the major browsers.

Simon Johnson
So write a loop that does it before returning true instead of using jQuery/
Max Schmeling
Bingo! You'll probably want some sort of delay. You'll probably have to experiment a little but my approach should work.
Simon Johnson
Is there some way to call a web service to delete the item from javascript? That way I could use the nice clean jQuery fade and not even have to post back?
Max Schmeling
A: 

Register the handler for form "submit" event.

$("form").submit(function() {

    // if user initiated delete action 
    // do your thing with deleted row (effects, etc.)

    // after you're done with it, submit the form from script
    // (you can queue the submission after the effect)
    // the submission from the script won't trigger this event handler

    return false; // prevent submission
}

Preventing form submission is necessary to avoid interference with the effects you want to perform. After they are finished, you are free to proceed with submission.

Marko Dumic
It's not exactly straight forward to tell that the user deleted an item in that code. I guess I could set a variable onclick on the button. Is that what you would suggest?
Max Schmeling
Yes, that would be one way to do it. But if you cannot easily diferentiate between various submit buttons, Just prevent form submission and attach different handlers to delete buttons and other (submit or edit) buttons. In "delete" handlers do your effects and then submit the form.
Marko Dumic
+2  A: 

You can use page methods in asp.net to send a request to the server without doing a postback. They are very simple to use and you can do whatever effect you like when the ajax call is completed (you get a function called on success).

If you want to stick with the post back one solution is the following:

<asp:Button id="myButton" OnClientClick="return fadeThenAllowSubmit()" ... />

and in js something like:

var allowSubmit = false;
function fadeThenAllowSubmit() {
    if (allowSubmit) return true
    // do the jquery stuff that will be completed in, let's say, 1000ms
    setTimeout(function() {
        allowSubmit = true
        $("input[id$=myButton]").click()
        allowSubmit = false
    }, 1000)
    return false
}

It's a bit of a hack, the idea is to cancel the postback initially, do some stuff then set a timer where the postback will be enabled. The big problem with this approach is that the fade effect and the actual delete are independent (in case of an error you still get the fade effect).

Aleris