ASP.net GridView不从FooterTemplate插入

时间:2011-06-07 10:27:07

标签: asp.net gridview

我被困住了。我实现了将GridView FooterTemplate中的复选框和文本框控件中的新DB值插入到SQLDataSource中所需的所有步骤,但是当我单击“添加”按钮以激活“插入”命令时,我的页面会闪烁并且不会发生插入。就实际的SQL而言,我有一个存储过程设置为GridView的DataSource的插入操作。我已经单独测试了这个程序,它运行正常。

我的设计基于这篇文章: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>

插入事件处理程序:

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

1 个答案:

答案 0 :(得分: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">