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

[Solved] Calling javascript method with parameters in clienttemplate not working

2 Answers 181 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alpesh
Top achievements
Rank 1
Alpesh asked on 20 Sep 2010, 07:48 PM
Hi,

I am creating columntemplate in telerik MVC grid with below code-

columns.Bound(a => a.Id).ClientTemplate("<a onclick='viewDetails('" + "'<#= Id #>'" + "');' href='javascript:'>View</a>").Title("View");

and this is my function in javascript -
function viewDetails(activityId) {
       //....Do stuff here...
    }

Now the issue that i am facing is, the javascript function viewDetails is not getting called and on page load i am getting javascript error and the description says "syntax error: expected ("

When i remove the method parameter and instead use rel attribute like -
columns.Bound(a => a.Id).ClientTemplate("<a onclick='viewDetails();' rel='<#= Id #>' href='javascript:'>View</a>").Title("View");

and write my method like -
function viewDetails() {
       var activityId = $(this).attr('rel'); //----- activityId is always null
       //....Do stuff here...
    }

The method gets called but  activityId is always null in the method

Can anyone please let me know how can i pass my row id from grid column template to the javascript method?

Thanks
Alpesh

2 Answers, 1 is accepted

Sort by
0
Adam Gritt
Top achievements
Rank 1
answered on 22 Sep 2010, 07:05 PM
I was able to get what you are trying to do working.  I think the problem is the single/double quote formatting you have going on.  Below is the code I used for rendering the grid.  My EmployeeItem object just has the two properties.  I was able to pass the Id as both the integer value like this or if you want it as a string then add a \" to either side of the <#=  #> and it will pass it in as a string.

<%= Html.Telerik().Grid<EmployeeItem>()
   .Name("TestGrid")
   .DataBinding(db =>
   {
      db.Ajax().Select("_AjaxBinding", "Home");
   })
   .Columns(col =>
   {
      col.Bound(c => c.EmployeeId).ClientTemplate("<a onclick='viewDetails(<#= EmployeeId #>);' href='javascript:void(0)'>View</a>").Title("View");
      col.Bound(c => c.EmployeeName).Title("Name");
   })
   .Pageable(pager =>
   {
      pager.PageSize(20);
   })
%>
  
<% Html.Telerik().ScriptRegistrar().Render(); %>
               
<script type="text/javascript">
   function viewDetails(activityId) {
      alert(activityId);
   }
</script>
0
Alpesh
Top achievements
Rank 1
answered on 23 Sep 2010, 03:48 AM
Tags
General Discussions
Asked by
Alpesh
Top achievements
Rank 1
Answers by
Adam Gritt
Top achievements
Rank 1
Alpesh
Top achievements
Rank 1
Share this question
or