Click here to Skip to main content
15,887,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir,

I made one dyanmic rows generated table in which I m able to generate the table, but unable to delete it, I m using javascript code for this the code is shown below:

I have written the code with debugger; because I debug this so guide me not counting debugger code I used

JavaScript
<Script type="text/javascript">
function deleteRow(tblPartner) {
            debugger;
            try {
                var table = document.getElementById(tblPartner);
                var rowCount = table.rows.length;
                for (var i = 0; i < rowCount; i++) {
                    var row = table.rows[i];
                    var chkBox = row.cells[0].childNodes[0];
                    if (null != chkBox && true == chkBox.checked) {
                        if (rowCount <=1) {
                            alert("Cannot delete all the rows.");
                            break;
                        }
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }
                }
            } catch (e) {
                alert(e);
            }
</Script>

my table id is tblpartner
I m using Html button tag to delete <input type="Button" runat="server" Value="Remove" önclick="deleteRow('tblPartner')"/> it is not deleting rows on by one taking the value of table null
tell me that I should use ArrayList to delete on row one by one

Please Guide me
Posted
Updated 15-Nov-11 1:10am
v2
Comments
Prashant Srivastava LKO 15-Nov-11 7:23am    
Hello Anil Sir How u change the text of the codes
thatraja 15-Nov-11 7:40am    
Click Improve question button

In .Net every id's changed when it will executed, for the same it will add a prefix to every id's............You can check it by
Ctrl+U
and change it according that. after that it will worked fine......
 
Share this answer
 
Are you trying to delete all but one row? If so, then it's working fine for me. If you want to delete ALL of the selected rows, even if it clears the table, you need to change

JavaScript
if (rowCount <=1) {


to

JavaScript
if (rowCount <=0) {


because you are preventing them from deleting the last row if the table only has 1 row left.

It works fine in my simple HTML file I used to test your snippet:

JavaScript
<html>
<head>
<Script type="text/javascript">
function deleteRow(tblPartner) {
   debugger;
   try {
       var table = document.getElementById(tblPartner);
       var rowCount = table.rows.length;
       for (var i = 0; i < rowCount; i++) {
           var row = table.rows[i];
           var chkBox = row.cells[0].childNodes[0];
           if (null != chkBox && true == chkBox.checked) {
               if (rowCount <=0) {
                   alert("Cannot delete all the rows.");
                   break;
               }
               table.deleteRow(i);
               rowCount--;
               i--;
           }
       }
   } catch (e) {
       alert(e);
   }
}
</Script>
</head>
<body>
<table border="1" id="tblPartner">
<tr>
<td><input type="checkbox" />1-1</td>
<td>1-2</td>
<td>1-3</td>
</tr>
<tr>
<td><input type="checkbox" />2-1</td>
<td>2-2</td>
<td>2-3</td>
</tr>
<tr>
<td><input type="checkbox" />3-1</td>
<td>3-2</td>
<td>3-3</td>
</tr>
</table>
<input type="Button"  runat="server" Value="Remove"  önclick="deleteRow('tblPartner')"/>
</body>
</html>
 
Share this answer
 
v2
Comments
Prashant Srivastava LKO 15-Nov-11 10:33am    
Yes u r absolutely right but sir When calling this function at button click event of html tag it is not working when I debug the code it was showing that table is null so that it jump to exception
joshd06 15-Nov-11 10:56am    
Sorry, perhaps I missed something from your description. I modified my answer with my full test file. Could you do the same and give a more full example showing the issue?
Prashant Srivastava LKO 16-Nov-11 4:41am    
this is my code in place of urs HTML tag

<asp:Table Width="100%" BorderWidth="0px" CellSpacing="2" ID="tblPartner" runat="server" Border="1"
CellPadding="4" Visible="False">
<asp:TableHeaderRow>
<asp:TableHeaderCell Width="10%" ID="H1" HorizontalAlign="Center" VerticalAlign="Middle"
Text="S.No">
<asp:TableHeaderCell Width="18%" ID="H2" HorizontalAlign="Center" VerticalAlign="Middle"
Text="StartTime<span style='color:red;'>*</span>">
<asp:TableHeaderCell Width="18%" ID="H5" HorizontalAlign="Center" VerticalAlign="Middle"
Text="EndTime<span style='color:red;'>*</span>">
<asp:TableHeaderCell ID="H3" HorizontalAlign="Center" VerticalAlign="Middle" Text="Project<span style='color:red;'>*</span>">
<asp:TableHeaderCell ID="H4" HorizontalAlign="Center" VerticalAlign="Middle" Text="Module<span style='color:red;'>*</span>">

<asp:TableHeaderCell ID="H6" HorizontalAlign="Center" VerticalAlign="Middle" Text="Task<span style='color:red;'>*</span>">

<asp:TableHeaderCell ID="H7" HorizontalAlign="Center" VerticalAlign="Middle" Text="WorkType<span style='color:red;'>*</span>">

<asp:TableHeaderCell ID="H8" HorizontalAlign="Center" VerticalAlign="Middle" Text="Status<span style='color:red;'>*</span>">

<asp:TableHeaderCell ID="H9" HorizontalAlign="Center" VerticalAlign="Middle" Text="Remarks<span style='color:red;'>*</span>">

<asp:TableHeaderCell ID="h66" HorizontalAlign="Center" VerticalAlign="Middle" Text="Remove<span style='color:red;'>*</span>">


<asp:TableRow>
<asp:TableCell ID="tc1" HorizontalAlign="Center" VerticalAlign="Middle" Text="1">

<asp:TableCell ID="tc2" HorizontalAlign="Center" VerticalAlign="Middle">
<asp:TextBox ID="txtStartTime" CssClass="txtbox" Width="60px" onblur="javascript:ComapareTime();" runat="server" MaxLength="150" />


<asp:RequiredFieldValidator ID="RFV_txtStartTime" runat="server" Display="Dynamic" ValidationGroup="AddMore"
ControlToValidate="txtStartTime">

<asp:TableCell ID="tc15" HorizontalAlign="Center" VerticalAlign="Middle">
<asp:TextBox ID="txtEndTime" CssClass="txtbox" Width="60px" onblur="javascript:ComapareTime();" runat="server" MaxLength="150" />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900