简体   繁体   中英

The FormView fired event ModeChanging which wasn't handled.

Ok, so I'm struggling with using asp:formview.

I've got the formview up and running and I've added the 'Edit' button.

<asp:FormView runat="server" id="fwHotelDetails" DataKeyNames="id" OnDataBound="fwHotelDetails_DataBound" OnModeChanging="fwHotelDetails_ModeChanging" >
    <ItemTemplate>
        // (..) some code here which outputs some data

        <asp:Repeater runat="server" id="repScore">
          <ItemTemplate>
            <span class="item"> Some output here</span>
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
          </ItemTemplate>
        </asp:Repeater>

        <EditItemTemplate>
             Test test, anything??
        </EditItemTemplate>

    </ItemTemplate>
</asp:FormView>

I've tried thefollowing solutions in the code behind - none of them works:

protected void fwHotelDetails_ItemCommand(object sender, FormViewModeEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        fwHotelDetails.ChangeMode(e.NewMode);
    }
}

and this:

protected void fwHotelDetails_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e)
{
    fwHotelDetails.ChangeMode((FormViewMode)e.NewMode);
}

Clicking the Edit button only gives me the following error message:

The FormView 'fwHotelDetails' fired event ModeChanging which wasn't handled

What more needs to be done?

This page is a great reference for FormView controller: http://authors.aspalliance.com/aspxtreme/sys/web/ui/webcontrols/FormViewClass.aspx

Update: I've updated code to refelct Phaedrus suggestion. Current status is that even after clicking Edit button, the content from ItemTemplate is loaded.

You have to specify which method handles the ModeChanging event. This event is raised when a FormView control attempts to switch between edit, insert, and read-only mode, but before the mode actually changes.

<asp:FormView OnModeChanging="fwHotelDetails_ModeChanging" />

The second parameter of your method signature is 'DetailsViewModeEventArgs' it should be 'FormViewModeEventArgs'.

void fwHotelDetails_ModeChanging(Object sender, FormViewModeEventArgs e)
{
}

Just Simply write code in formview's Item_Command

protected void formview_ItemCommand(object sender, FormViewCommandEventArgs e)

        {

            if (e.CommandName == "Edit")
            {
                formview.DefaultMode = FormViewMode.Edit;
                formview.DataBind();
            }
            if (e.CommandName == "Cancel")
            {
                formview.DefaultMode = FormViewMode.ReadOnly;
                formview.DataBind();
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM