2011-06-07 76 views
1

我被卡住了。我實現了從GridView FooterTemplate中的複選框和文本框控件插入新數據庫值到SQLDataSource所需的所有步驟,但是當單擊我的「添加」按鈕來觸發插入命令時,我的頁面會閃爍並且不會發生插入。就實際的SQL而言,我有一個存儲過程設置爲GridView的DataSource的插入操作。我已經單獨測試過程,它工作正常。ASP.net GridView不能從FooterTemplate插入

我基於這篇文章的設計:http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx我的錯誤可能在我的事件處理程序的某處。任何想法我失蹤?

這將是太長,張貼在GridView控件代碼的,所以這裏的要點:

FooterTemplate爲插入按鈕:

<asp:GridView ID="CartonGridView" runat="server" AutoGenerateColumns="False" 
      CellPadding="6" DataKeyNames="CartonID" Width="100%" 
      DataSourceID="Carton_Table" ForeColor="#333333" 
      GridLines="None" AllowSorting="True"> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <Columns> 
       <asp:TemplateField ShowHeader="False"> 
        <EditItemTemplate> 
         <asp:Button ID="Button1" runat="server" CausesValidation="True" 
          CommandName="Update" Text="Update" /> 
         <asp:Button ID="Button2" runat="server" CausesValidation="False" 
          CommandName="Cancel" Text="Cancel" /> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Button ID="Button1" runat="server" CausesValidation="False" 
          CommandName="Edit" Text="Edit" /> 
        </ItemTemplate> 
        <FooterTemplate> 
         <asp:Button ID="Button1" runat="server" CausesValidation="True" 
          CommandName="Insert" Text="Add" /> 
         <asp:Button ID="Button2" runat="server" CausesValidation="False" 
          CommandName="Cancel" Text="Cancel" /> 
        </FooterTemplate> 
       </asp:TemplateField> 
       ... 

的SqlDataSource綁定到的GridView:

<asp:SqlDataSource ID="Carton_Table" runat="server" 
      ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
      InsertCommand="spCartonInsert" InsertCommandType="StoredProcedure" 
      SelectCommand="spCartonSelect" SelectCommandType="StoredProcedure" 
      UpdateCommand="spCartonUpdate" UpdateCommandType="StoredProcedure"> 
      ... 
      <InsertParameters> 
       <asp:Parameter Name="Active"    Type="Boolean" /> 
       <asp:Parameter Name="Value"  Type="String" /> 
       <asp:Parameter Name="Description"  Type="String" /> 
      </InsertParameters> 

插入事件句柄rs:

protected void CartonGridView_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 

      if (e.CommandName == "Insert") 
      { 
       CheckBox Active = CartonGridView.FooterRow.FindControl("InsertActive") as CheckBox; 
       TextBox SAPCode = CartonGridView.FooterRow.FindControl("InsertSAPCode") as TextBox; 
       TextBox Description = CartonGridView.FooterRow.FindControl("InsertDescription") as TextBox; 

       SqlParameter paramActive = new SqlParameter("@Active", SqlDbType.Bit); 
       paramActive.Direction = ParameterDirection.Input; 
       paramActive.Value = Active.Checked; 
       insertParameters.Add(paramActive); 

       SqlParameter paramValue = new SqlParameter("@Value", SqlDbType.NVarChar, 30); 
       paramValue.Direction = ParameterDirection.Input; 
       paramValue.Value = paramValue.Text; 
       insertParameters.Add(paramValue); 

       SqlParameter paramDescription = new SqlParameter("@SAP_Long_Description", SqlDbType.NVarChar, 250); 
       paramDescription.Direction = ParameterDirection.Input; 
       paramDescription.Value = Description.Text; 
       insertParameters.Add(paramDescription); 

       Carton_Table.Insert(); 
      } 
     } 

     protected void Carton_Table_Inserting(object sender, SqlDataSourceCommandEventArgs e) 
     { 
      e.Command.Parameters.Clear(); 
      foreach (SqlParameter p in insertParameters) 
       e.Command.Parameters.Add(p); 
     } 
+0

sql insert命令(sqldatasource)在哪裏?也許有錯誤? – Aristos 2011-06-07 10:36:43

+0

@Aristos - 我添加了指定插入過程的GridView引用的代碼,但正如我之前所說的,SP本身工作正常。 – BradV 2011-06-07 13:43:42

回答

0

是的,事件處理程序沒有連線。

<asp:GridView ID="CartonGridView" runat="server" AutoGenerateColumns="False" 
      CellPadding="6" DataKeyNames="CartonID" Width="100%" 
      DataSourceID="Carton_Table" ForeColor="#333333" 
      GridLines="None" AllowSorting="True" 
onrowcommand="CartonGridView_RowCommand"> 
+0

這樣做!非常感謝gbs。爲了記錄,我還必須將OnInsert處理程序連接到我的Carton_Table_Inserting()。 – BradV 2011-06-08 01:31:49