k80sg
k80sg

Reputation: 2473

Deleting Gridview rows using Javascript

I have a javascript function to clear my form which includes a gridview, I previously tried another way to delete the rows without postback but somehow when I tried to add a new row, all the previous bounce back. Maybe I am using viewstate to maintain my gridview, not too sure but below is what I am doing which works well but somehow it only deletes one row, I guess probably when the postback occurs, the loop got wiped out. Any advice? I need the loop to delete all my gridview rows. Thanks!

 for (i = 0; i < camGv.rows.length; i++) {
                window.__doPostBack('ctl00$cphBody$gvCamVoucher', 'Delete$0');
            }

Upvotes: 0

Views: 3895

Answers (2)

k80sg
k80sg

Reputation: 2473

Thanks for the answer, I clear the gridview rows from code behind and use an Update Panel to group the grid so that the postback do not in any way affect my other fields.

Upvotes: 0

Marwan
Marwan

Reputation: 2402

Well i think you need 2 things

1- A Way to Delete Your Rows from the Grid in the Client Side

2- A way to make that deletion effect on the server side just in case you made any postbacks to stay in your deletion state

1- you can catch the grid view in the client side with id and clear the all inside its execpt if there is a footer that have the paging like this code

document.getElementById('grid view id').getElementsByTagName('TBODY').innerHTML='';

this line of script deletes all your rows thats if you dont have a footer grid pager

2- you have to make an ajax call to contact with the server and make the real effect if that table is have some thing from the database i think u need a delete function to delete all the rows from the database , if that is just a temp table have nothing to do from the database so i suggest to put it inside a Session that you can access the session in the ajax call

JavaScript

$.ajax({
        type: "POST",
        //web method path
        url: "pageName.aspx/webmethodName",
        data: "{action:'delete'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function(result) 
           {
           }
        ,error: function(error)
           {
           }
});

ASP.NET CS

[WebMethod]

public string DeleteAll(string action)
{
//you have to put the table from the begining where ever you initialize the data on it inside a session
DataTable dt = (DataTable)HttpContext.Current.Session["tbl"];
dt.Rows.Clear();
HttpContext.Current.Session["tbl"]=dt;
}

Regards please any question ask me :)

Upvotes: 3

Related Questions