Item Templates And Adding New Items

4 posts, 0 answers
  1. Robert
    Robert avatar
    23 posts
    Member since:
    Feb 2009

    Posted 16 Feb 2011 Link to this post

    I have an ItemTemplate defined for my RadListBox. My list box is bound to the results of a Linq to SQL Query. In this case, it's bound to objects of type MyObject.

    I'd like to be able to add a new RadListIBoxItem defined by the user. Ideally, I'd like to create a new MyObject instance and call myListBox.Items.Add(new RadListBoxItem(myNewObject)). This would then create a new item and bind the values in the ItemTemplate appropriately.

    Of course, this isn't an option. I can only pass a text and value to the new RadListBoxItem, and this of course won't map to many of the controls in my ItemTemplate.

    So what's the correct approach here? Is this simply the wrong control to use?
  2. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 21 Feb 2011 Link to this post

    Hi Robert,

    In order to add templates dynamically, you should use ITemplate interface and its InstantiateIn method.
    The general approach is the following, you "MyObject" class should implement the above mention interface and put your own implementation of the InstantiateIn method , which has input parameter , the container where it will be instantiated, the listbox item in your case. I've prepared a sample page showing how to implement this scenario.
    I hope this would help you out.


    Best wishes,
    Dimitar Terziev
    the Telerik team
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Robert
    Robert avatar
    23 posts
    Member since:
    Feb 2009

    Posted 21 Feb 2011 Link to this post

    I'm not sure this gets me what I need.

    The "MyObject" I refered to has several properties I need to display in the list box, not just a Text and a Value. So I can't simply rely on those two properties exposed by the RadListBoxItem.

    I tried referencing the DateItem property inside my InstantiateIn method, but while this works initially, after any subsequent postbacks I'm left with an empty item again since the DataItem property doesn't survive the postbacks like the Text and Value properties do.

     

     

    public void InstantiateIn(Control container)

     

     

    {

     

     

     

        object dataItem = ((RadListBoxItem)container).DataItem;

     

     

     

     

        someControl.Text = (string)DataBinder.Eval(dataItem, "Property1");
        someOtherControl.Text = (string)DataBinder.Eval(dataItem, "Property2");
        anotherControl.Text = (string)DataBinder.Eval(dataItem, "Property3");

     

     

        container.Controls.Add(someControl);
        container.Controls.Add(someOtherControl);
        container.Controls.Add(anotherControl);

     

    }

    I was sure to assigned IDs to the controls I'm adding so that ViewState is preserved between postbacks, but I must be missing something.

    Ideas?

  5. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 22 Feb 2011 Link to this post

    Hello Robert,

    You are not limited only to the Text and Value of the listbox items, since you could bind numerous values to custom attributes. Every listbox item support custom attributes, which are name-value pairs, so try adding the necessary amount of custom attributes and bind values to them, as you are binding the text and value. This demo here demonstrates how you could bind data to custom attributes.

    Greetings,
    Dimitar Terziev
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top