Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormView doesnt enter in insert mode. Why?

<asp:FormView ID="FormView1" runat="server" AllowPaging="True" 
        DataSourceID="SqlDataSource1" OnModeChanging="FormView1_ModeChanging">
        <EditItemTemplate>
            ID :
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <br />
            CpuName :
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            Status :
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </EditItemTemplate>
        <EmptyDataTemplate>
            There is no data.
        </EmptyDataTemplate>
    <ItemTemplate>
     <table>
      <tr>
        <td align="right"><b>ID: </b></td>       
        <td><%# Eval("Id") %></td>
      </tr>
      <tr>
        <td align="right"><b>CPUName:</b></td>     
        <td><%# Eval("cpuname") %></td>
      </tr>
      <tr>
        <td align="right"><b>Status</b></td>      
        <td><%# Eval("status") %></td>
      </tr>

    </table>        
    <asp:LinkButton CommandName="Edit" runat="server">Edit</asp:LinkButton>
    <asp:LinkButton CommandName="Insert" runat="server">Insert</asp:LinkButton>
    </ItemTemplate>
    <InsertItemTemplate>
     <table>
      <tr>
        <td align="right"><b>ID: </b></td>       
        <td>
            <asp:TextBox ID="txtFVID" runat="server"></asp:TextBox></td>
      </tr>
      <tr>
        <td align="right"><b>CPUName:</b></td>     
        <td>
            <asp:TextBox ID="txtFVName" runat="server"></asp:TextBox></td>
      </tr>
      <tr>
        <td align="right"><b>Status</b></td>      
        <td>
            <asp:TextBox ID="txtFVStatus" runat="server"></asp:TextBox></td>
      </tr>

    </table>        

    </InsertItemTemplate>
    </asp:FormView>

protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
    {
            if (e.NewMode == FormViewMode.Edit)
            {
                FormView1.ChangeMode(FormViewMode.Edit);
            }
            if (e.NewMode == FormViewMode.Insert)
            {
                FormView1.ChangeMode(FormViewMode.Insert);
            }

    }

I get the following error:- FormView 'FormView1' must be in insert mode to insert a new record.

like image 661
sly_Chandan Avatar asked Mar 06 '26 17:03

sly_Chandan


1 Answers

Add

<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="New" />

into your <ItemTemplate>.

And move your

<asp:LinkButton CommandName="Insert" runat="server">Insert</asp:LinkButton>
    </ItemTemplate>

to <InsertItemTemplate> where it belongs to.

like image 200
Kirill Avatar answered Mar 08 '26 06:03

Kirill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!