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

Dynamic Template creation for custom control

2 Answers 84 Views
Rotator
This is a migrated thread and some comments may be shown as answers.
Mark Greve
Top achievements
Rank 2
Mark Greve asked on 18 Sep 2008, 07:00 PM
I am having problems creating a ItemTemplate for a user control.

The following code is what I have roughed out and can't get it to work. I am trying to create an anchor based on the the dataTable I have bound to the the RadRotator.


Here is my template code
Thanks in advance.

public class RotatorTemplate : ITemplate

{

private Control _container;

public void InstantiateIn(Control container)

{

_container = container;

Literal contents =

new Literal();

contents.DataBinding +=

new EventHandler(contents_DataBinding);

container.Controls.Add(contents);

}

private void contents_DataBinding(object sender, EventArgs e)

{

Literal contents = sender

as Literal;

contents.Text =

"<a href=&quot;<%# originalAttribute="href" originalPath="&quot;<%#" originalAttribute="href" originalPath="&quot;<%#" DataBinder.Eval(contents.BindingContainer, &quotDataItem.LinkUrl&quot;)%>&quot; target=&quot;_blank&quot;> <img src=&quotIMG/<%# originalAttribute="src" originalPath="&quotIMG/<%#" originalAttribute="src" originalPath="&quotIMG/<%#" DataBinder.Eval(contents.BindingContainer, &quot;DataItem.ImageUrl&quot;) %>&quot; alt=&quot;&quot; style=&quot;border:0px;&quot/></a>";

}

}

}

2 Answers, 1 is accepted

Sort by
0
Lini
Telerik team
answered on 19 Sep 2008, 07:29 AM
Hello,

I noticed in your code (the contents_DataBinding() function) that you are still using the following syntax:

<%# DataBinder.Eval(contents.BindingContainer, "DataItem.LinkUrl")%>

This can only work if your template is defined in an ASP.NET form or user control. Since you are creating your template in the code, you can use the value directly (concatenate it as a string):

contents.Text = "<a href='"+DataBinder.Eval(contents.BindingContainer, "DataItem.LinkUrl")+"'>link</a>";


Greetings,
Lini
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mark Greve
Top achievements
Rank 2
answered on 19 Sep 2008, 04:31 PM
Thanks got it solved.
Tags
Rotator
Asked by
Mark Greve
Top achievements
Rank 2
Answers by
Lini
Telerik team
Mark Greve
Top achievements
Rank 2
Share this question
or