Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a listView inside an update panel.
The OnItemUpdating event, which is handled in ModulesList_ItemUpdating() is as follows:
C#
protected void ModulesList_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
        {
           //add data into the database
           ModulesList.EditIndex = -1;
           ModulesList.DataBind();
        }


The edited data is being added to database, but ListView remains in edit mode.
The same lines, when added in onItemCancelling event, works perfectly.
C#
protected void ModulesList_ItemCancelling(object sender, ListViewCancelEventArgs e)
     {
         ModulesList.EditIndex = -1;
         ModulesList.DataBind();
     }


I inserted breakpoints for debugging in the onItemUpdated event, the editIndex=-1 is executing but not reflected in the web page.

What am I missing? I have been trying for more than a day :-/
Posted
Updated 24-Mar-13 0:57am
v3

Figured it out . I hadn't included
C#
e.cancel=true;
at the end of ItemUpdating event handler. Now it works perfectly inside update panel. The modified function is:
C#
protected void ModulesList_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
        {
           //add data into the database
           ModulesList.EditIndex = -1;
           ModulesList.DataBind();
           e.cancel=true;
        }
 
Share this answer
 
check if it works outside the update panel..
 
Share this answer
 
Comments
WajihaAhmed 24-Mar-13 11:10am    
It works if I remove update panel, but i need to use update panel to prevent full page refresh :-/
Probably EditIndex[^] value is -1 (which indicates that no item is being edited) and that's why you can't set it.
Try to add try... catch[^] block to catch errors, especially ArgumentOutOfRangeException[^] exception.
C#
protected void ModulesList_ItemCancelling(object sender, ListViewCancelEventArgs e)
     {
         try
         {
         ModulesList.EditIndex = -1;
         ModulesList.DataBind();
         }
         catch (ArgumentOutOfRangeException ex)
         {
         //show error message         
         }
         catch (Exception ex)
         {
         //show error message
         }
     }
 
Share this answer
 
Comments
Maciej Los 24-Mar-13 7:08am    
???
WajihaAhmed 24-Mar-13 7:19am    
To switch from edit mode to display mode, EditIndex needs to be set to -1. Reference: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.editindex.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900