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

column.Template with expressions in Q1 2010

3 Answers 260 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
cheburek
Top achievements
Rank 1
cheburek asked on 17 Mar 2010, 06:33 PM
Hello
I'm using the Q1 2010 of Telerik controls for ASP .Net MVC and stuck with next when on adding columns to grid:

Following code worked in Q3 2009 version (I have removed such things as Encoded(false) in samples):
columns.Add ( s => String.IsNullOrEmpty ( s.StringVar1 )                    
                    ? s.StringVar2 
                    : Html.ActionLink<SomeController> ( x => x.Title ( s.Var3 ), s.StringVar1, null ) ); 

With Q1 version that code I've changed first to:
columns.Bound ( s => String.IsNullOrEmpty ( s.StringVar1 )                    
                    ? s.StringVar2 
                    : Html.ActionLink<SomeController> ( x => x.Title ( s.Var3 ), s.StringVar1, null ) ); 

but got telerik exception "Bound columns require a field or property access expression."
And after that changed to template:

columns.Template ( s => String.IsNullOrEmpty ( s.StringVar1 )                    
                    ? s.StringVar2 
                    : Html.ActionLink<SomeController> ( x => x.Title ( s.Var3 ), s.StringVar1, null ) ); 

but get compile error: "Only assignment, call, increment, decrement and new object expressions can be used as a statement"

I think that it is possible to wrap that condition to the html helper and return only string - something like this:
columns.Template ( Html.MyWrapHelper<SomeController> ( x => x.Title ( s.Var3 ), s.StringVar1, null ) ); 

but this is very simple example and in more complex cases there will be many helpers with functionality where hardly to understand there difference.

So the question: Is it possible to work with column.Template(...) where between parenthesis present not a string but expression which returns string (see examples above)?

Thanks

3 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 18 Mar 2010, 08:48 AM
Hello cheburek,

The Template column expect and Action which should not return a result. Rather it should output its value to HTML:

columns.Template ( s =>
{
if (String.IsNullOrEmpty ( s.StringVar1 ) ) {
%>
<%= s.StringVar2 %>
<%
} else {
%>
<%= Html.ActionLink<SomeController> ( x => x.Title ( s.Var3 ), s.StringVar1, null ) %>
<%
}
});

All the best,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Rodolfo Gonzalez
Top achievements
Rank 1
answered on 08 Apr 2010, 12:41 AM
I would just like to add how to use the colum.template funcionality in VB.NET 2008 because only up until VB 2010 can lambda expressions be decaulred as Sub instead of as Functions.

I was basing my code on Atanas' helpful post regarding VB.NET coding for the MVC grid (http://www.telerik.com/community/forums/aspnet-mvc/grid/mvc-grid-using-vb-net.aspx), but it doesn't explain or show how to use the template function.

So I was desperately trying to make this work:

gridBuilder.Columns(Function(columns) columns.Bound(Function(o) o.mtikNumero) _ 
.Title("Ticket #") _ 
.Template(Function(o) Html.ActionLink(o.mtikNumero, "Ticket"New With {.id = o.mtikId}))) 
 

And then I understood that the template function actually has to write the string you compose to the response buffer.

This is easily doable on VB 2010 because it now supports lamdas that don't return a value (as in .Template(Sub(x) Response.Write(x.field))...

But not on VB 2008... 

So after tinkering around, I was able to trick the rendering enginge with this code:

<script runat="server"
    '...page load and declarations go here... 
 
    Protected Function TrickWrt(ByVal s As StringAs String 
        Response.Write(s) 
        Return "" 
    End Function 
</script> 
 
gridBuilder.Columns(Function(columns) columns.Bound(Function(o) o.mtikNumero) _ 
.Title("Ticket #") _ 
.Template(Function(o) TrickWrt(Html.ActionLink(o.mtikNumero, "Ticket"New With {.id = o.mtikId}).ToHtmlString))) 
 

I hope this helps others using VB 2008 trying to use Telerik's MVC extensions...
0
Niladri
Top achievements
Rank 1
answered on 23 Dec 2010, 08:46 AM
hi,
please can you provide the same code for razor engine

columns.Template(u =>{
if (u.Status == 3){
@<img alt="Processed" src="@Url.Content("~/Content/Images/1-done2.png")/>
}
else if (u.Status == 1)
{
@<img alt="In Process" src="@Url.Content("~/Content/Images/1-done3.png")/>
}

}).Width(50).HtmlAttributes(new { style = "text-align:center" })


This gives me an error :

Compiler Error Message: CS1002: ; expected

Source Error:


Line 83: 
Line 84: @<img alt="Processed" src="@Url.Content("~/Content/Images/1-done2.png")" />
Line 85:
Line 86: }
Line 87:
Tags
Grid
Asked by
cheburek
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Rodolfo Gonzalez
Top achievements
Rank 1
Niladri
Top achievements
Rank 1
Share this question
or