4

I created a detailsview which appears after a date is selected in a calender. This detailsview is filled via a select statement in code behind. Here is a list of my problems i encouter:

  1. I have to click twice on the edit button to get the detailsview in Edit mode.
  2. Once in edit mode i see the update and cancel button, but I also have to click twice on cancel to get back to the read only state.
  3. When I'm in Edit mode i have 3 rows which can be editted, when I press cancel twice in readOnly mode the values of these 3 rows aren't shown anymore, they're just empty fields although the data is still in the database.
  4. When I change something in Edit mode and press Update, my detailsview just dissapears.

Code-behind:

protected void DetailView1_ModeChanging(Object sender, DetailsViewModeEventArgs e)
{
    if (e.NewMode == DetailsViewMode.Edit)
    {
        DetailsView1.ChangeMode(e.NewMode);
    }
    if (e.CancelingEdit)
    {
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);   
    }
}

protected void DetailView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e)
{
    DetailsView1.DataBind();
}

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
    DetailsView1.DataBind();
}

Markup:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
    OnModeChanging="DetailView1_ModeChanging" OnItemUpdating="DetailView1_ItemUpdating"
    OnItemUpdated="DetailsView1_ItemUpdated"
    AllowPaging="True" PageSize="5" HeaderText="Agenda"  CellPadding="10" 
    ForeColor="#333333" />
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Dieter
  • 401
  • 1
  • 9
  • 31

1 Answers1

1

Your problem is in DetailView1_ModeChanging

The ModeChanging event is raised when a DetailsView control attempts to change between edit, insert, and read-only mode, but before the CurrentMode property is updated. This allows you to provide an event handler that performs a custom routine, such as canceling the mode change, whenever this event occurs.

From MSDN

you need to use Item Command event

protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
    }
}

Edit:

 protected void DetailView1_ModeChanging(Object sender, DetailsViewModeEventArgs e)
    {
        if (e.NewMode == DetailsViewMode.Edit)
        {
            DetailsView1.ChangeMode(e.NewMode);
            DetailsView1.Datebind(); // add this and check
        }
        if (e.CancelingEdit)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);   
            DetailsView1.Datebind(); // add this and check
        }
    }
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • Hello, thank you for your reply. I added `OnItemCommand="DetailsView1_ItemCommand` to my detailsview and used the code you wrote down above. At first I put all my code in text, then I got the message it was firing the ModeChanging event which wasn't handled so I put that method back as code to use. The problem remains, I still have to click twice before I go to Edit mode and Editing doesn't work. – Dieter Apr 28 '11 at 11:13
  • I have tried this and when I run the program my detailsview dissapears from the screen when I press the Edit button. The same problem occurs when I use a gridview instead of detailsview. – Dieter Apr 29 '11 at 07:57