Tuesday, March 18, 2008

DetailsView Edit Mode never changes after Update command. DetailsView Edit button requires to clicks to change to edit mode.

I was stocked for sometime using the asp.net 2.0 DetailsView control to Edit/Update data. I wanted to do my custom implementation for the Update command instead of using the DataBinding Object sources but I found my self in trouble with several road blocks.

Problem 1:
When clicking on the Edit button the DetailsView will not change the mode until the second click. The edit button required two clicks to change the mode to Edit instead of ReadOnly.
I solved this problem by changing the mode in the ModeChanging event handler and then rebinding the DetailsView control.


ModeChanging Event Handler:

protected void dvOrder_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
dvOrders.ChangeMode(e.NewMode);

if (e.NewMode != DetailsViewMode.Insert)
{
RefreshOrderDetails();
}
}




Problem 2:
When clicking the update button I handle the updating of the order in the ItemUpdating event but the ItemUpdated never fires and the DetailsView never change the mode to ReadOnly after the data was updated. DetailsView control always stayed on the Edit Mode.


I solve this problem by canceling the ItemUpdating event, changing the mode, and rebinding the details view data source.


ItemUpdating Event Handler:

protected void dvOrder_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{

UpdateOrder();

e.Cancel = true;

dvOrders.ChangeMode(DetailsViewMode.ReadOnly);

RefreshOrderDetails();
}



This was the only way I got things to worked for me. The ItemUpdated event never fired but I got the DetailsView to works how I wanted. I wish there was an easier way to do this or at least more documentation on Microsoft’s website.

6 comments:

  1. hi, Javier
    Thank this text, it help me to deal with some problems which i meet.
    I am a developer for .net.
    Do you make friends with me and discuss some skill problems.
    Thank you.

    contact me:
    chpz1980@hotmail.com
    chpz1980@gmail.com

    ReplyDelete
  2. Javier,

    Thank you so much for this clever solution.

    God bless you..

    :))

    ReplyDelete
  3. Help! I tried the solution you suggested, but still have to click the "edit" button twice for it to go into edit mode. Here's my piece of VB code. Any thoughts? Thanks!

    Protected Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) Handles DetailsView1.ModeChanging

    DetailsView1.ChangeMode(e.NewMode)
    If e.NewMode <> DetailsViewMode.Insert Then
    Dim intIndex As Integer
    intIndex = CInt(CType(DetailsView1.FindControl("lblIndexDV"), Label).Text)
    LoadDataList(intIndex)
    End If

    End Sub

    ReplyDelete
  4. Try handling your LoadDataList(index) via DetailsView.ItemInserting() event.

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.iteminserting.aspx

    ReplyDelete
  5. Hey, that was very helpful, Thanks!

    I still have a 3rd problem... When i click the "Cancel" button after clicking "Edit", it's not working

    ReplyDelete
  6. Hey I was wondering if you could just help me.....I know XQuery is XML Query....but what is JQuery????????? Please provide information over it. Provide links to related topics if possible.

    ReplyDelete