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

Accessing DataItem and EditItemTemplate controls from OnItemEditing

2 Answers 332 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Bruno
Top achievements
Rank 2
Bruno asked on 19 Nov 2009, 08:47 PM

Given a ListView with an ItemTemplate, InsertItemTemplate, and EditItemTemplate, how can I access the controls in the EditItemTemplate before they are first displayed?

<telerik:RadListView ID="ContactsList" runat="server" DataSourceID="ContactsDataSource"   
            GroupItemCount="4"   
            DataKeyNames="EntityContactID" 
            OnItemEditing="ContactsList_ItemEditing" 
            AllowMultiItemEdit="false" 
            > 

ContactsDataSource is a LinqDataSource with EnableUpdate=true

            <ItemTemplate> 
                <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# DataBinder.Eval( Container, "DataItem.Name" ) %>'   
                        ForeColor="Blue" CommandName="Edit" CausesValidation="false" /> 
 


protected void ContactsList_ItemEditing(object sender, RadListViewCommandEventArgs e)  
    {  
        if (e.ListViewItem.ItemType == RadListViewItemType.DataItem)  
        {  
            RadListViewDataItem dataItem = (RadListViewDataItem)e.ListViewItem;  
             

Here, dataItem.DataItem is null, e.ListViewItem.FindControl() only finds controls in the ItemTemplate and not the EditItemTemplate and e.ListViewItem.IsInEditMode is false.

There is a LinkButton in the EditItemTemplate I need to access and modify before the edit form is shown to the user.

2 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 20 Nov 2009, 11:43 AM
Hi Bruno,

At the point of which ItemEditing event is raised, the RadListView is actually not yet switch to edit mode. Therefore a better place to access controls from EditTemplate is inside ItemCreated event as shown bellow:

protected void RadListView1_ItemCreated(object sender, RadListViewItemEventArgs e)
  {
      if (e.Item is RadListViewEditableItem && e.Item.IsInEditMode)
      {
          var myControl = e.Item.FindControl("MyControlID");
      }
  }


Sincerely yours,
Rosen
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Erik
Top achievements
Rank 2
answered on 10 Oct 2010, 06:31 PM

 

 

 

 

 

protected void rlv_ProductFile_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)

 

{

 

 

if (e.Item.ItemType.Equals(RadListViewItemType.DataItem) || e.Item.ItemType.Equals(RadListViewItemType.AlternatingItem))

 

{

 

 

RadBinaryImage DataImage = (RadBinaryImage)e.Item.FindControl("DataImage");

 

 

 

RadListViewDataItem dataItem = ((RadListViewDataItem)e.Item).DataItem;

 

}

}

 

 


give this compiler error :
Error 1 Cannot implicitly convert type 'object' to 'Telerik.Web.UI.RadListViewDataItem'. An explicit conversion exists (are you missing a cast?) D:\Dev\KeepOnTrack\WebUserControls\Shop\ShowProductFile.ascx.cs 40 48 KeepOnTrack


please help

Tags
ListView
Asked by
Bruno
Top achievements
Rank 2
Answers by
Rosen
Telerik team
Erik
Top achievements
Rank 2
Share this question
or