ASP.net GridView not Inserting from FooterTemplate

我的梦境 提交于 2019-12-13 02:17:32

问题


I'm stuck. I implemented all steps needed to insert new DB values from a checkbox and textbox controls in a GridView FooterTemplate into a SQLDataSource, but when I click my "Add" button to fire the Insert command, my page flashes and no insert occurs. As far as the actual SQL is concerned, I have a stored procedure set to the GridView's DataSource's insert action. I have separately tested the procedure and it works fine.

I based my design on this article: http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx My error is probably somewhere in my event handlers. Any idea what I'm missing?

It would be too long to post all of the GridView code, so here are the essentials:

FooterTemplate for insert button:

    <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 Binding to 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>

Insertion Event Handlers:

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:


Yep, the event-handler is not wired-up.

<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">


来源:https://stackoverflow.com/questions/6263891/asp-net-gridview-not-inserting-from-footertemplate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!