Tuesday, 4 March 2014

ASP.NET Insert data in GridView using asp.net


In this article I will explain How to save GridView FooterTemplate (Footer Template) Data in ASP.NET.

Below design code and aspx.cs code is useful for saving records to Database(Insert into DB) by using ASP.NET Gridview FooterTemplate.

The GridView control is a feature rich and versatile control used to accept, display, and edit data on a web page. It is a commonly used control in ASP.Net web applications. 

To use a GridView control a DataSource control has to be attached to the GridView control. The property DataSourceID of the GridView control binds the GridView control to the DataSource control and allows paging, sorting and database operations with the DataSource. 

First of all we need to design footer template for new records saving purpose. Below is the code for page design.

ASPX Page design :



<asp:GridView ID="gvContact" runat="server" AutoGenerateColumns="false"
                OnRowCommand="gvContact_RowCommand" OnRowEditing="gvContact_RowEditing"
                AllowPaging="true" PageSize="20" OnRowCancelingEdit="gvContact_RowCancelingEdit"
                OnRowDataBound="gvContact_RowDataBound"
                OnRowDeleting="gvContact_RowDeleting">
                <Columns>
                    <asp:TemplateField HeaderText="Operations">
                        <FooterTemplate>
                            <asp:LinkButton runat="server" ID="btnAdd" Text="Add" CommandArgument="<%# Container.DataItemIndex %>"
                                CommandName="Add" ToolTip="Add" ValidationGroup="Add" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Main No" HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Label ID='lblMainNo' runat="Server" Text='<%# Eval("MainNo") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID='txtMainNoFT' runat="Server" Width="150px">
                            </asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name" HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Label ID='lblName' runat="Server" Text='<%# Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID='txtNameFT' runat="Server" Width="150px">
                            </asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Email" HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Label ID='lblEmail' runat="Server" Text='<%# Eval("Email") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID='txtEmailFT' runat="Server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

ASPX.CS CODE FOR RECORD SAVING PURPOSE:

protected void gvContact_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Add")
        {
            DataTable dtADDMessage = new DataTable();

            TextBox txtMainNoFT = this.gvContact.FooterRow.FindControl("txtMainNoFT") as TextBox;
            TextBox txtNameFT = this.gvContact.FooterRow.FindControl("txtNameFT") as TextBox;
            TextBox txtEmailFT = this.gvContact.FooterRow.FindControl("txtEmailFT") as TextBox;

            objclass.ActionTender = 1;
            objclass.MainNo = txtMainNoFT.Text;
            objclass.Name = txtNameFT.Text;
            objclass.Email = txtEmailFT.Text;
            dtADDMessage = objclass.AddInfo();

            if ((dtADDMessage == null))
            {
                gvContact.EditIndex = -1;
                gvContact.DataSource = dtADDMessage;
                gvContact.DataBind();
                string message = "Data Saved successfully !";
                string script = "<script language=\"javascript\"  type=\"text/javascript\">;alert('" + message + "');</script>";
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "AlertMessage", script, false);
            }
            else
            {

            }
        }

}

No comments:

Post a Comment