开发者

ASP.net GridView not Inserting from FooterTemplate

开发者 https://www.devze.com 2023-03-11 09:36 出处:网络
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 th

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号