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

Calling Javascript Function from a Grid Template Column

1 Answer 533 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 06 May 2011, 10:35 AM
Hi,

Just seem to have hit a wall with this one. I have a grid, and in that grid is the following column:

                                    <telerik:GridTemplateColumn UniqueName="PostNCAuditGuid" HeaderText="Post NC Audit">
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:HyperLink NavigateUrl="javascript:createPostNCAudit();" runat="server" ID="hlCreatePostNCAudit"
                                                Text="Create a Post NC Audit"></asp:HyperLink>
                                            <asp:HyperLink NavigateUrl="javascript:viewPostNCAudit();" runat="server" ID="hlViewPostNCAudit"
                                                Text="View Post NC Audit"></asp:HyperLink>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>

Now, I need the createPostNCAudit() and the viewPostNCAudit() functions to have a value passed to them indicating the row that this hyperlink was clicked on. As the 'rowclicked' event is not fired, I have no way of pulling an Id for the row into some sort of variable prior to them clicking one of the hyperlinks. Of course they may click the row and THEN click the hyperlink, but in the scenario of them simply clicking straight on the hyperlink I don't know how to identify the row the call came from.

Any help would be greatly appreciated. Whether you have a way of me passing a value to the function in the navigateurl property or assigning the row id to a variable I will be made up.

Thanks a lot,

Dan

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 May 2011, 12:25 PM
Hello Daniel,

One approach is to pass the RowIndex to the along with the function name.For thet try the following.
  • Hook the ItemCreated event.
  • Access the control and set the NavigateUrl from there.
Here is the sample code.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
      if (e.Item is GridDataItem)
      {
          GridDataItem item = (GridDataItem)e.Item;
          HyperLink hp = (HyperLink)item.FindControl("hlCreatePostNCAudit");
          hp.NavigateUrl = "javascript:createPostNCAudit("+e.Item.ItemIndex+")";
       }
   }

Javascript:
function createPostNCAudit(index)
   {
       alert(index);
   }

Thanks,
Shinu.
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or