Click here to Skip to main content
15,119,363 members
Articles / Programming Languages / Javascript
Article
Posted 5 Mar 2005

Stats

85.9K views
894 downloads
13 bookmarked

Adding and deleting multiple selected rows in a table using JavaScript

Rate me:
Please Sign up or sign in to vote.
1.79/5 (8 votes)
12 Mar 2005
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.

JavaScript
<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

 
GeneralProblems using firefox Pin
hmarchant7-Feb-07 5:03
Memberhmarchant7-Feb-07 5:03 
GeneralDelete multiple rows Pin
theoronis22-Jun-06 7:25
Membertheoronis22-Jun-06 7:25 

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

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