Dynamic Template creation for custom control

3 posts, 0 answers
  1. Mark Greve
    Mark Greve avatar
    11 posts
    Member since:
    Aug 2008

    Posted 18 Sep 2008 Link to this post

    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. Lini
    Admin
    Lini avatar
    2144 posts

    Posted 19 Sep 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mark Greve
    Mark Greve avatar
    11 posts
    Member since:
    Aug 2008

    Posted 19 Sep 2008 Link to this post

    Thanks got it solved.
Back to Top