This is a migrated thread and some comments may be shown as answers.

accessing the row and cell index to delete a row on client side

3 Answers 338 Views
Grid
This is a migrated thread and some comments may be shown as answers.
getset
Top achievements
Rank 1
getset asked on 11 Nov 2010, 05:57 AM
Hi, 

I have a hyper link column in my radgrid and I use <href=" "> for that. Its a "Remove" hyperlink  which when clicked should delete the particular row on the client side. How can I access the cell of the column where the hyperlink is clicked. I tried to set the onclick event of the hyperlink to a function where I set the command name as Remove and in the Onrowselected event I check for the condition if the command="Remove" and then delete the row but this doesn't work as the Onrowselected event doesn't fire when I click on the hyperlink. When I click the hyperlink it just goes to the Onclick event of that and stops there. 

I need a way to call the Onrowselected event when I click the hyperlink(href link) or if that is not possible then I need to delete the row in the Onclick client event of the hyperlink and for that I should be able to access the particular cell of the row.

FYI: I need to delete the rows on client side and I use the html href for the hyperlink as I had some issues using the telerik hyperlink column.

<Telerik:GridTemplalteColumn UniqueName="Remove">
<ItemTemplate>
<a href="javascript:void(0);" OnClick=OnRemove();> <u>Remove</u> </a>
</ItemTemplate>
</Telerik:GridTemplalteColumn >

function OnRemove()
{
command="Remove";
}

function OnRowSelected(){
//functionality to get the grid and delete the row here
}

Please suggest a way to do this.

Thank you,
Vik

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Nov 2010, 08:03 AM
Hello,

Try the following approach to get the row which is going to delete. First you need to access the HtmlAnchor from code behind. For that add runat="server" and an Id to that control in mark-up. Then in ItemCreated event access the control and attach  'onclick' client event by passing the item index to event handler. In that event handler identify the corresponding grid row using this ItemIndex value.

ASPX:
<telerik:GridTemplateColumn UniqueName="Remove">
   <ItemTemplate>
       <a href="#" id="Remove" runat="server"><u>Remove</u> </a>
   </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           System.Web.UI.HtmlControls.HtmlAnchor link = (System.Web.UI.HtmlControls.HtmlAnchor)item.FindControl("Remove"); // accessing 'HtmlAnchor'
           link.Attributes.Add("onclick", "OnRemove('" + item.ItemIndex + "');");
       }
   }

Java Script:
<script type="text/javascript">
    function OnRemove(index) {
        var grid = $find("<%=RadGrid2.ClientID %>");
        var MasterTable = grid.get_masterTableView();
        var row = MasterTable.get_dataItems()[index]; // accessing corresponding grid row
        . . . . . . .
    }
</script>

Thanks,
Princy.
0
getset
Top achievements
Rank 1
answered on 11 Nov 2010, 02:53 PM
Hi, Thanks for the reply. I don't get the intelligence for the griditemeventargs in the code behind. How do I get it??
0
getset
Top achievements
Rank 1
answered on 13 Nov 2010, 11:24 PM
Hi princy,

I tried your approach, but it doesn't work. When I click on the Remove hyperlink, nothing happens, it doesn't go into the OnRemove event handler  at all.
Tags
Grid
Asked by
getset
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
getset
Top achievements
Rank 1
Share this question
or