Apply Style to InsertItemTemplate

5 posts, 0 answers
  1. ToltingColtAcres
    ToltingColtAcres avatar
    91 posts
    Member since:
    Nov 2010

    Posted 10 Jan 2013 Link to this post

    I have a radlistview bound to a data source.

    When the user edits or inserts an item, I make modifications dynamically to the edit/insertitemtemplate (hiding some options, making others read only) based upon other values contained on the web page.

    I have been doing this in the ItemDataBound event, and it works great.

    Unless the datasource contains no records.

    In this case, the EmptyItemTemplate is displayed, and when I click Insert button I have placed on the template, the ItemDataBound event is never fired.

    The InsertItemTemplate is displayed with default styles, etc.

    What event and RadListViewItem object can I use to dynamically configure the insertitemtemplate dynamically when there are no records associated in the underlying data source?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Jan 2013 Link to this post


    Try accessing the controls as shown below.
      protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
        if (e.Item is RadListViewInsertItem)
                RadListViewInsertItem item = (RadListViewInsertItem)e.Item;
                TextBox txt = (TextBox)item.FindControl("TextBox1");
                //apply styles here

  3. ToltingColtAcres
    ToltingColtAcres avatar
    91 posts
    Member since:
    Nov 2010

    Posted 11 Jan 2013 Link to this post


    As indicate in my original post, that works fine.

    *IF* the underlying data source contains data.

    If the underlying data source does not have any records, the ItemDataBound event is not fired.

    I need an event I can intercept where I can modify the InsertItemTemplate? For example, is there a way to modify the controls in the template in the ItemCommand event? e.g.:

    (unfortunately, the below code throws a null reference exception because, I assume, the InsertItemTemplate hasn't been created yet... but I don't know what other event to use. The ItemDataBound event is not fired because the underlying datasource has no records.
    protected void RadListView1_ItemCommand(Object sender, RadListViewCommandEventArgs e)
            switch (e.CommandName)
                case "InitInsert":
                    RadListView1.InsertItemPosition = RadListViewInsertItemPosition.FirstItem;
                    RadListViewInsertItem item = RadListView1.InsertItem;
                    ((RadTextBox)item.FindControl("EntryDate")).Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
                    RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None;
  4. ToltingColtAcres
    ToltingColtAcres avatar
    91 posts
    Member since:
    Nov 2010

    Posted 13 Jan 2013 Link to this post

    As a followup to my own post, I created a RadListView and captured every event with a breakpoint... it seems the ItemCreated event is the proper event to use, not the ItemDataBound event. The only caveat is I needed to test to make sure I had an addressable RadListItem type which allowed me to access controls... so, for example, I had to effectively test:


    if (!(e.Item.ItemType == RadListViewItemType.EditItem || e.Item.ItemType == RadListViewItemType.InsertItem)) return;

    to avoid attempting to set styles on non-existant controls...

    This worked for applying styles on both items i needed to edit, as well as on inserted items, regardless of whether or not there was a record in the underlying dataset.





  5. Maria Ilieva
    Maria Ilieva avatar
    4017 posts

    Posted 16 Jan 2013 Link to this post

    Hello Michael,

    Thank you for updating us on this thread and sharing your approach.
    Let us know if you have other questions or further help is needed.

    Maria Ilieva
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top