2011-07-12 49 views
2
<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不會在插入模式下輸入。爲什麼?

回答

7

添加

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

到您的<ItemTemplate>

並移動

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

<InsertItemTemplate>它所屬。

+0

萬人迷....絕對眼罩。做得好!!! –

+0

不客氣!我很高興這很有幫助。 – Kirill

2

你可以做到這一點在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); 
     } 
} 
+0

都能跟得上它,然後詢問InsertCommand並將其參數 –

0

CommandName更改爲FormView爲插入模式爲「新建」,而不是「插入」。 「插入」CommandName是在插入模式下執行數據源控件的實際插入方法。

嘗試:

<asp:LinkButton CommandName="New" runat="server">New</asp:LinkButton> 
相關問題