views:

668

answers:

1

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.

+1  A: 

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)
{
}
Phaedrus
Ah.... ok, I added this. Now I get the following error: No overload for 'fwHotelDetails_ModeChanging' matches delegate 'System.Web.UI.WebControls.FormViewModeEventHandler'. I've tried google to see what it means... but can't find any good answer.
Steven
ok, that worked. But I'm not able to change mode I think. Because when I click the Edit button, it still tried to load the content from <ItemTemplate>
Steven
I'm marking this as solved and I'll start a new thread for the mode problem. Thanks.
Steven