views:

65

answers:

3

I have a site that show infomation that are contained in div blocks. The info is taken from a database so the user can change the info or delete it.

When the user deletes a div block i want to use jQuery to animate that its disappearing and also that its deleted from the database. How can i do this without doing a postback, i dont want the site to reload, i just want the Div block to disappear visually and be gone from the database at the same time.

Would i use Ajax update panel to achieve this ?

+3  A: 

Something like this...

$.ajax({ ... 
success : function(){ 
    var target = $('#MyDeletedElement');
    target.fadeOut('fast',function(){
        target.remove();
    });
});
Master Morality
is there anyway to call a function that i have in the .cs file ? I would like to use linq to do the database action.
eski
+1  A: 

Although the overall example is written with php, this should give you a basic idea of how to accomplish what you're trying to do.

http://papermashup.com/jquery-ajax-delete/

This link will show how to call a WebMethod in your codebehind file with JQuery

http://www.codedigest.com/Articles/ASPNETAJAX/185_Using_JQuery_in_ASPNet_AJAX_Applications%E2%80%93Part_2.aspx

Robert Williams
this is what I'm looking for but i do not understand it. I'm using asp.net for this, i would like to call a function that i have in the .cs file, if its possible.
eski
I've added another link to hopefully shed light on how to call a WebMethod from JQuery.
Robert Williams
+5  A: 

try this. You will have to alter the expression at the start and create a webservice to handle the callback.

 <script type="text/javascript" language="javascript">
      var expression = "div.deleteable";

      jQuery(function($) {
           $(expression).click(function(){
              var itemId = $(this).attr("Id");
              $.ajax({ type: "POST",
                       url: "YourPage.aspx/DeleteItem",
                       data: "{'Id' :'" + itemId + "'}",
                       dataType: "json",
                       contentType: 'application/json; charset=utf-8',
                       success: function(json) {
                          $(this).slideUp('fast',function(){
                             //gone - perhaps report to the user that delete was successful,
                             //by accessing jason.d
                                var result = eval("(" + json.d + ")");

                              $(this).remove();
                            });
                        },
                        timeout: 5000,
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            if (textStatus == 'timeout') {
                               alert('timeout');
                            }
                            //Other error handlers here
                        }
                 });
           });
       });

 </script>

in your page codebehind file:

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public static string DeleteItem(string Id)
    {
        //Returns a json  string with success message 
        var result = YourBusinessLayerClass.DeleteItem(Id);  
        return result;
    }
Daniel Dyson