Databinding events not firing using ITemplate

9 posts, 0 answers
  1. Samantha
    Samantha avatar
    50 posts
    Member since:
    May 2008

    Posted 23 Dec 2008 Link to this post

    I have a class implementing ITemplate for the rad rotator (inside a user control).  The InstantiateIn method is called and all of the controls are created fine.  However, the Databinding events in the implemented ITemplate class don't fire:

    [code]

    <

    telerik:RadRotator ID="radRotateNews" runat="server"

     

     

    Skin="Vista"

     

     

    ScrollDuration="3000" FrameDuration="2000" RotatorType="FromCode"

     

     

    Width="470px" Height="312px" CssClass="RadRotator_Default"

     

     

    AppendDataBoundItems="true" >

     

    </

    telerik:RadRotator>

     



    protected

    void Page_Load(object sender, EventArgs e)

     

    {

     

     

    if (!IsPostBack)

     

    {

     

    DataTable dt = GetScreens();

     

    radRotateNews.DataSource = dt;

    CreateNewsScreens(dt);

    radRotateNews.DataBind();

    }

    }


    protected

    void CreateScreens(DataTable dt)

     

    {

     

    if (dt.Rows.Count > 0)

     

    {

     

    for (int i = 0; i < dt.Rows.Count; i++)

     

    {

     

    string screenType = dt.Rows[i]["ScreenTypeDesc"].ToString();
    RadRotatorItem newsScreen = new RadRotatorItem();

     

    newsScreen.ItemTemplate =

    new NewsRotatorTemplate(screenType, dt.Rows.Count);

     

    radRotateNews.Items.Add(newsScreen);

    }

    }

     

    else

     

    {

     

    //show a default screen/image

     

    }

    }



    public

    class NewsRotatorTemplate : ITemplate

     

    {

     

    private string templateType;

     

     

    private int numberScreens;

     

     


    public
    NewsRotatorTemplate(string screenType, int noScreens)

     

    {

     

    //

     

     

    // TODO: Add constructor logic here

     

     

    //

     

     

    this.templateType = screenType;

     

     

    this.numberScreens = noScreens;
    }

     

     

    public void InstantiateIn(Control container)

     

    {

    ....
    Label

    lblScreenNo = new Label();

     

    lblScreenNo.ID =

    "lblScreenNo" + screenNo;

     

    lblScreenNo.DataBinding +=

    new EventHandler(lblScreenNo_DataBinding);

     

    container.Controls.Add(lblScreenNo);

    ....
    }

     

    void

    lblScreenNo_DataBinding(object sender, EventArgs e)

     

    {

     

    Label lblScreenNo = (Label)sender;

     

     

    RadRotatorItem container = (RadRotatorItem)lblScreenNo.NamingContainer;

     

     

    string fieldValue = DataBinder.Eval(container.DataItem, "ScreenNo").ToString();

     

    lblScreenNo.Text = fieldValue;

    }


    [/code]

    Any ideas?

  2. Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 26 Dec 2008 Link to this post

    Hi Samantha,

    I tried to replicate your logic and to built-up a sample test demo but I was not able to this because the functions GetScreens() and CreateNewsScreens() are missing.

    I am not quite sure that I completely understand your scenario but for your convenience I prepared a sample project which demonstrates how the RadRotator control can be programmatically binded to a DataTable data source and set to it a custom ItemTemplate.

    Please examine my project and if you still have problems, modify it according to your setup, open a new support ticket and send it back to us. Once we have a better understanding of your requirements, we will do our best to help.
     
    All the best,
    Fiko
    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. Samantha
    Samantha avatar
    50 posts
    Member since:
    May 2008

    Posted 29 Dec 2008 Link to this post

    Sorry, the CreateNewsScreens is actually the CreateScreens method, I edited the names of some of the methods before posting to make them a little more generic.  The example code you attached is not in a user control. 
  5. Samantha
    Samantha avatar
    50 posts
    Member since:
    May 2008

    Posted 29 Dec 2008 Link to this post

    It appears I don't need to create the RadRotatorItem, I changed my code to be like yours, attaching the template to the whole rotator rather than an individual RadRotatorItem.  For future reference, can you dynamically create RadRotatorItems, each with a different template (dynamically)?
  6. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 30 Dec 2008 Link to this post

    Hi Samantha,

    Yes, this is possible - the template concept that is used in RadRotator is the ASP.NET's one. Basically whatever you can do with a standard template control like asp:repeater is possible with RadRotator as well.

    More information on how to work with ASP.NET templates is available in many resources in the Net. For example you can check these links:
    http://msdn.microsoft.com/en-us/magazine/cc301573.aspx
    http://msdn.microsoft.com/en-us/library/y0h809ak(VS.71).aspx



    Regards,
    Georgi Tunev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. Samantha
    Samantha avatar
    50 posts
    Member since:
    May 2008

    Posted 30 Dec 2008 Link to this post

    It did not work when I tried to create the RadRotatorItems programmatically (see my code above), assigning a template to each RadRotatorItem and not the RadRotator as suggested.  How do I assign a template dynamically to one RadRotatorItem?
  8. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 05 Jan 2009 Link to this post

    Hi Samantha,

    I am sorry if my answer was not precise enough - I meant to provide a general information on RadRotator's basics.

    What I tried to explain is that RadRotator's databinding logic is not custom but we are using the general ASP.NET one - whatever is possible in ASP.NET when working with templates, is possible with RadRotator as well.
    This being said, I suggest to implement the desired logic not by using RadRotator, but a standard asp:repeater control. Once you have your code working as expected, the same approach will work with RadRotator as well.



    Regards,
    Georgi Tunev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  9. Samantha
    Samantha avatar
    50 posts
    Member since:
    May 2008

    Posted 07 Jan 2009 Link to this post

    That's not really answering my question - I am asking about programatically.  When I tried to assign an ItemTemplate to a RadRotatorItem, that did not work.  I had to assign them template to the RadRotator itself for the code I posted to work.  What code do I need to use (in C#) to assign a template to a RadRotatorItem and not a RadRotator?
  10. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 13 Jan 2009 Link to this post

    Hello Samantha,

    This task is not directly related to the RadRotator control. As we pointed out the templates and the binding mechanism of RadRotator are identical with asp:repeater ones - they are the both using the framework's logic. Again we suggest to implement the desired behavior by using a repeater. Once you have your logic working as expected - just send it to us in a support ticket and we will rework it so it uses RadRotator.



    Sincerely yours,
    Georgi Tunev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017