FormView未进入插入模式.为什么

本文关键字:模式 为什么 插入 FormView | 更新日期: 2023-09-27 18:02:04

<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);
            }
    }

我得到以下错误:-FormView"FormView1"必须处于插入模式才能插入新记录。

FormView未进入插入模式.为什么

添加

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

进入您的<ItemTemplate>

移动

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

到它所属的<InsertItemTemplate>

您可以在ItemCommand事件中执行此操作,例如。。。

protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
       if (e.CommandName == "Edit")
        {
            FormView1.ChangeMode(FormViewMode.Edit);
        }
       else if (e.CommandName == "Insert")
        {
            FormView1.ChangeMode(FormViewMode.Insert);
        }
}
FormView更改为Insert模式的CommandName是"New",而不是"Insert"。"Insert"CommandName用于在Insert模式下执行数据源控件的实际Insert方法。

尝试:

<asp:LinkButton CommandName="New" runat="server">New</asp:LinkButton>