开发者

DetailsView not updating and changing only after 2 clicks, Cancel not showing editable values in readonly

开发者 https://www.devze.com 2023-03-01 01:46 出处:网络
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:

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, DetailsViewUpdated开发者_如何学GoEventArgs 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" />


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
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消