Example:
@(Html.Telerik().Grid<RSBBADetail>() .Name("BBaGrid") .DataKeys(keys => { keys.Add(o => o.ProjectNumber).RouteKey("id"); }) .DataBinding(dataBinding => { dataBinding.Ajax() .Select("_SelectDetailsAjax", "RequiredActions"); }) .Columns(columns => { columns.Bound(o => o.ProjectNumber).ClientTemplate(Html.CustomExtension("<#= ProjectNumber #>"); })or as a lamda expression since it doesn't seem like "<#= #>" would be interpreted until it is passed to ClientTemplate?
.Columns(columns => { columns.Bound(o => o.ProjectNumber).ClientTemplate(Html.CustomExtensionFor(o => o.ProjectNumber));Thanks,
Chris
10 Answers, 1 is accepted
This is not possible. You cannot pass a JavaScript variable to a server-side method. Please describe in more detail what you are trying to achieve - I am sure there are better ways to do it.
Atanas Korchev
the Telerik team
I'm trying to create a template column in an AJAX grid that uses an HTML helper or some other function to generate the cell content. The helper/function would be able to access the values of the members passed to it so that it could generate the content according to those values.
It would look something like one of the two columns in this grid:
@(Html.Telerik().Grid<RSBBADetail>() .Name("BBaGrid") .DataKeys(keys => { keys.Add(o => o.ProjectNumber).RouteKey("id"); }) .DataBinding(dataBinding => { dataBinding.Ajax() .Select("_SelectDetailsAjax", "RequiredActions"); }) .Columns(columns => { /* method 1 */
columns.Bound(o => o.ProjectNumber).ClientTemplate(o => Html.CustomExtension(o.ProjectNumber, o.ProjectName));
/* method 2 */
columns.Template(o => Html.CustomExtension(o.ProjectNumber, o.ProjectName)); }) It is possible to do this (using the second method) with a non-AJAX grid and I am wondering if there is a way to accomplish the same thing in a grid that uses AJAX.
Thanks again for your help.
How does your helper method work? If it embeds correct client-side expressions it may work even this way:
Html.CustomExtension("<#= ProjectNumber #>"This must return some string which contains "<#= ProjectNumber #>" in order for the client template to be properly evaluated in the browser. Regards,
Atanas Korchev
the Telerik team
Ajax grids seem to completely ignore the Template() column builder. If they didn't then that seems like it would solve my problem.
This is by design as Template is executed on the server side whereas ClientTemplate is executed in the browser. There is no way server code to node the value of a JavaScript object. You cannot mix server code with JavaScript parameters.
Regards,Atanas Korchev
the Telerik team
ClientTemplate("<img src='" + Url.Content(Html.IconPath("<#= Name #>").ToString()) + "' alt='<#=DocumentType.Name#>' style='vertical-align: middle;'/>" + Html.ActionLink("<#=Name#>", "GetDocument", "Quote", new { id = "<#=DocumentId#>" }, null).ToString());The helper method is Html.IconPath. The problem is that the value being passed in to the helper is literally <#= Name #> instead of the document name.
As I said in my previous reply you can't execute server-side code on the client-side. You need to either use server binding or translate your helper method to JavaScript so it can be executed client-side.
Regards,Atanas Korchev
the Telerik team
a[href $='.rtf']{ padding-right: 18px; background: transparent url("../Images/icons/icon_doc.gif") no-repeat center left;}This CSS rule will be applied only for <a> elements whose href attribute ends with .rtf. Does your link's generated href end with rtf?
Regards,Atanas Korchev
the Telerik team
columns.Bound(o => o.Name).ClientTemplate(Html.ActionLink("<#=Name#>", "GetDocument", "Quote", new { id = "<#=DocumentId#>" }, new { title = "<#= Name #>" }).ToString());a[title $='.pdf']{ padding-right: 28px; background: transparent url("../Images/icons/icon_pdf.gif") no-repeat center right;}