9

I have FormView:

<asp:FormView ID="fvReport" runat="server" DefaultMode="ReadOnly" AllowPaging="false" OnModeChanging="fvReport_ModeChanging" DataKeyNames="id">

protected void fvReport_ModeChanging(Object sender, FormViewModeEventArgs e)
    {
        switch (e.NewMode)
        {
            case FormViewMode.Edit:
                fvReport.AllowPaging = false;
                break;
        }
    }

in ItemTamplate I put LinkButton:

<asp:LinkButton ID="lbEdit" runat="server" CausesValidation="true" CommandName="Edit" CommandArgument='<%# Eval("id") %>'>Редактировать</asp:LinkButton>

Of course, FormView has EditItemTemplate section.

When I click Button, FormView is refreshed and stays in ReadOnly. What am I doing wrong?

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
DmitryB
  • 147
  • 1
  • 3
  • 7

3 Answers3

13

you have to call ChangeMode method of FormView and pass the mode

fvReport.ChangeMode(DetailsViewMode.Edit);
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • 2
    thanks, in result I did as you) but I thought that FormView goes to edit mode automatically if CommandName set "Edit". [See MSDN](http://msdn.microsoft.com/en-gb/library/system.web.ui.webcontrols.formviewmodeeventargs%28v=VS.90%29.aspx) – DmitryB Apr 21 '11 at 05:31
0

Another option which I commonly use to go into edit mode from a formView is to add and define the EditItemTemplate element. This makes it a lot easier to make your application editable.

Within your formView you may need to change your DefaultMode to Edit. Also in your code behind try:

protected void fvReport_ModeChanging(Object sender, FormViewModeEventArgs e)
{

}

protected void lbEdit_Click(object sender, EventArgs e)
{
    LinkButton lbEdit = (LinkButton)fvReport.FindControl("lbEdit");

    if (sender == lbEdit)
    {
        fvReport.DataBind();
        fvReport.ChangeMode(FormViewMode.Edit);
    }
}
Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85
Scanner
  • 597
  • 1
  • 9
  • 19
0

There could be other reasons why your FormView isn't switching over. It's usually down to badly formatted HTML. Your designer sometimes tells you of malformed sections by displaying something like this...

enter image description here

On those occasions when you don't get this obvious message, FormView not switching over is usually down to something a little less obvious, such as bad AssociatedControlId attributes. I would recommend looking at you labels, validators and anything where a control has to be associated to another. Something as small as this...

<asp:Label runat="server"
    ID="labelAccessGrantedBy"
    Text="Access Granted By"
    AssociatedControlID="textAccessGranted" />
<asp:TextBox runat="server"
    ID="textAccessGrantedBy"
    CssClass="wmioSmall wFull"
    Text='<%# Bind("access_granted_by") %>' />

Notice the deliberate use of textAccessGranted above, rather than the actual textAccessGrantedBy TextBox? That's where the command handling has failed for me in the past.

Paul
  • 4,160
  • 3
  • 30
  • 56