3

I've a gridview in which there is an add and remove row facility.I want to knoow how could i remove a particular gridview row when corresponding remove button is clicked.

I've searched everywhere but nothing find quite useful to me

Heres my code

<asp:TemplateField>
  <ItemTemplate>
     <asp:LinkButton ID="gdlbtnRemove" runat="server" 
     OnClientClick="RemoveRow(this)">Remove</asp:LinkButton>
  </ItemTemplate>
</asp:TemplateField>

This is my javascript code

<script type="text/javascript">
  function RemoveRow(rowindex,objref)
  { 
    var row=objref.parentNode.parentNode;
    row.Remove();  
  }
 </script>

Im new to javascript........

1

2 Answers 2

4

Try this:

apsx:

<ItemTemplate> 
    <asp:LinkButton  ID="gdlbtnRemove" runat="server"     
     OnClientClick="return RemoveRow(this)">Remove</asp:LinkButton> 
</ItemTemplate>

Javascript:

function RemoveRow(item) {
    var table = document.getElementById('myGridView');
    table.deleteRow(item.parentNode.parentNode.rowIndex);
    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Here the problem is after clicking the remove button corresponding gridview row is disappearing first and then reappearing
This could happen if the click event bubbles up and the page posts back. The return RemoveRow(this) in the LinkButton definition combined with returning false on the handler function should stop the bubbling of the this event.
1

Old post i know but Tsachi's Answer was almost perfect for me except

This

var table = document.getElementById('GridviewID');

had to become this

var table = document.getElementById("<%= GridviewID.ClientID %>");

In case anyone else looks at this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.