Question

we just started with Kentico and are now testing a bit. One thing we're stuck on is showing data in transformations.

We have a custom table like Author. It hase a ID field, FirstName and SurName (both text). Book is a documenttype and has an ID, Title and a dropdown where we can select an Author.

On a page a have a datalist where i show book with a previewtransformation like this:

<div style="text-align:center;padding: 8px;margin: 4px;border: 1px solid #CCCCCC">

    <h2>
    <%# Eval("Title") %> 
    </h2>
      Author: <%# Eval("Author.FirstName") %>
    </div>

Now we want to show the name of the Author but when using <%# Eval("Author") %> it's showing the ID. We found out that we can use a custom function and return the name, but isn't there another way? Let's say we not only want to show the author's name, but also address, email and so on... Do we really need to create an method for each property we want to show?

Thanks in advance,

Bjorn

Was it helpful?

Solution

No, you can't drill into related tables in this way, because the data of an author is simply not in the data source you are displaying with the data list. But you don't have to create function for each property of an author you want to display. You may just create a function which will return whole author object, which is in your case CustomTableItem. The function may look like this.

public CustomTableItem GetAuthor(object id)
{
    int authorId = ValidationHelper.GetInteger(id, 0);        
    var pr = new CustomTableItemProvider();
    var item = pr.GetItem(authorId, "customtable.author"); 

    return item;
}

Then in a transformation you will use GetValue() method to get the value.

Author: <%# GetAuthor(Eval("AuthorID").GetValue("FirstName")) %>

Be aware of each call of the function will issue a database request, so i would suggest to use some kind of caching. Either output cache for whole page or you may implement some caching mechanism directly inside the function.

The other option you also have is to use CustomQueryRepeater/DataSource and write your own SQL query where you join book data with author data. Then you could use simply <%# Eval("FirstName") %> directly in yout transformation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top