Click here to Skip to main content
Click here to Skip to main content

Adding and deleting multiple selected rows in a table using JavaScript

, 12 Mar 2005
Rate this:
Please Sign up or sign in to vote.
This article will help the user in adding rows to a table in ASP.NET and deleting multiple selected rows from the table using JavaScript.

Usage

Take a hidden control on the webpage ("hdhidden") and set its default value to 1 and runat = server. 1 is added to its value when a new row is added in a table but does not change when a row is deleted from the table. Following are the two functions which perform addition and deletion of rows in a table.

<script language="javascript"> 
function deleteTableRows() 
{
    var counts=parseInt(document.all("hdhidden").value);
    //variable counts will contains the value of hidden control 
    //("hdhidden")
    var i;
    var a=0;
    for(i=1;i<counts;i++)
    {
        itemCheck="chk_"+i; 
        //itemCheck contians the checkboxes id. 
        if(document.all[itemCheck]!=undefined) 
        { 
            //if itemCheck is not undefined then adding 1 in "a" 
            //value
            a = a + 1; 
            //check whether is checkbox is checked or not
            if(document.all[itemCheck].checked==true) 
            { 
                //if chekbox is checked then compare 
                //"a" with "i"
                if(a!=i) 
                {
                    //if "a" is not equals to i then delete row 
                    //from the table on the basis of "a" value.
                    tblvalue.deleteRow(a); 
                } 
                else 
                {
                    //else delete row from the table on the 
                    //basis of "i" value.
                    tblvalue.deleteRow(i); 
                } 
                //less 1 from "i" and "a" values.
                i = parseInt(i) - 1;
                a = a - 1; 
            }
        }
    }
    return false; 
}

function AddItem() 
{ 
    var counts=parseInt(document.all("hdhidden").value); 
    //varibale counts will contains the value 
    //of hidden control (hdhidden)
    var Row,Cell;
    var CurrentPosition=parseInt(counts);
    //asigned value of variable "counts" to varibale "CurrentPosition" 
    Row = window.tblvalue.insertRow();
    Cell = Row.insertCell(); 
    //insert a cell in a row;
    //insert a checkbox in a cell with the name 
    //"chk_" as prefix + its current positon 
    //so thant checkbox has unique id
    Cell.innerHTML="<input type=checkbox name=chk_" + 
       CurrentPosition +" id=chkbox_"+ CurrentPosition+"; value='C'>"; 
    Cell = Row.insertCell(); 
    //insert another cell to row
    //insert a textbox in a cell with the name 
    //"txtval1_" as prefix + its current position
    Cell.innerHTML="<input style=Font-size=11; type=Text name=txtval1_" 
                    + CurrentPosition +" id=txtval1_"+ CurrentPosition+>"; 
    //insert another cell in a row
    Cell = Row.insertCell(); 
    //insert a textbox in a cell with the name 
    //"txtval2_" as prefix + its current position
    Cell.innerHTML="<input style=Font-size=11; type=Text name=txtval2_" + 
                        CurrentPosition +" id=txtval2_"+ CurrentPosition+>"; 
    //update the value of hidden control (hdhidden) by adding 1 
    //to its previous value
    document.all("hdhidden").value = parseInt(counts) + 1; 
    return false; 
} 
</script>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Raman Tayal
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralDelete multiple rows Pinmembertheoronis22-Jun-06 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 12 Mar 2005
Article Copyright 2005 by Raman Tayal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid