开发者

Gridview Row Delete

I am having a Gridview. I had placed a delete button in the last column of the row. whenever i click the delete button, the particular row should be deleted from the grid and also from the database. Here is my aspx code

<script language="javascript" type="text/javascript">
    function DeleteEmployeeDeta开发者_StackOverflowils(roleId) 
{
   if (confirm('Are you sure you want to delete this role?')) 
 {
 $.get("DeactivateRole?roleId=" + roleId,
 function () 
 {
     $('#row' + roleId).remove(); 
      //or
     $('#button' + roleId).parent().parent().remove(); 
 });
    }
}    function loadRoleList()
{
    $.ajaxSettings.cache = false;
    $.get(roleListActionUrl, roleList_callBack);
}

//This method is used to render the role details in the role List div
function roleList_callBack(data) {
    data = data + '<div id="AddRole"></div>';
    $("#EmpDetails").html(data);
}

This is my controller

 public ActionResult DeactivateRole(string roleId)
    {
       // What should i do here for Row Delete 
       return View();
    }


When I do something similar with AJAX I do two things:

  1. Delete in database
  2. Delete the row using javascript on callback e.g.

    function DeleteEmployeeDetails(roleId) { if (confirm('Are you sure you want to delete this role?')) { $.get("DeactivateRole?roleId=" + roleId, function() {

           //roleList_callBack();
    
    
    
       $('#row' + roleId).remove(); //Grab the row and delete it
    
    
       //If you don't have specific Id's for every row but your button does you could use the $.parent() function.
    
    
       $('#button'+roleId).parent().parent().remove(); //This finds the parent of the parent of the button and removes it. You can use as many parents as you'd like until you get a hold on the actual row, though this isn't always recommended due to if you change the design in some way your parent might not be correct. 
    
    
    });
    
    }

    }

If the delete succeeds in your database the row disappears instantly and you don't have to reload all the data from the database.

How you delete a row in your database depends on which framework you use. If you use ADO.NET Entity Framework you could do something like this:

public ActionResult DeactivateRole(string roleId) {

   My_Namespace.Models.DataContext dc = new My_Namespace.Models.DataContext();

   dc.DeleteObject(dc.Roles.FirstOrDefault(r => r.ID == roleId)); 
   dc.SaveChanges();

   return View();
}

The DeleteObject() method takes an actual database-entity-object as argument which it uses to delete the corresponding entry in you database.

The argument I use(dc.Roles.FirstOrDefault(r => r.ID == roleId)) gets the object from the database Roles-table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜