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

Apply Style to InsertItemTemplate

4 Answers 89 Views
ListView
This is a migrated thread and some comments may be shown as answers.
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
ToltingColtAcres asked on 10 Jan 2013, 04:47 PM
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?

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Jan 2013, 05:47 AM
Hi,

Try accessing the controls as shown below.
C#:
  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
        }
}

Thanks,
Princy
0
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
answered on 11 Jan 2013, 01:48 PM
Yes.

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();
                break;
            default:
                RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None;
                break;
        }
    }
0
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
answered on 13 Jan 2013, 09:46 PM
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.

 

 

 

 

0
Maria Ilieva
Telerik team
answered on 16 Jan 2013, 09:29 AM
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.

Regards,
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.
Tags
ListView
Asked by
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
Answers by
Princy
Top achievements
Rank 2
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
Maria Ilieva
Telerik team
Share this question
or