Click here to Skip to main content
15,068,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used template field in my grid view ,but when i attach the sqldatasource to it, all these fiedls change to bounded field, basic motive of my code is to insert data into database by grid view

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InsertComamnd.aspx.cs" Inherits="InsertComamnd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title><script runat="server">

void Button1_Click(object sender, EventArgs e)

{

TextBox Item_id = GridView1.FooterRow.FindControl("txtitemid") as TextBox;

TextBox Item_name = GridView1.FooterRow.FindControl("txtitemname") as TextBox;

TextBox Price = GridView1.FooterRow.FindControl("txtprice") as TextBox;

TextBox Quantity = GridView1.FooterRow.FindControl("txtquantity") as TextBox;



SqlDataSource1.InsertParameters["Item_id"].DefaultValue = Item_id.Text;

SqlDataSource1.InsertParameters["Item_name"].DefaultValue = Item_name.Text;

SqlDataSource1.InsertParameters["Price"].DefaultValue = Price.Text;

SqlDataSource1.InsertParameters["Quantity"].DefaultValue = Quantity.Text;





SqlDataSource1.Insert();

}

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            Width="237px" ShowFooter="True" BackColor="#DEBA84" BorderColor="#DEBA84"
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
            <Columns>
                <asp:TemplateField>
                      <ItemTemplate>
                               <asp:Label ID="lblitemid" runat="server"><%# Eval("Item_id") %></asp:Label>
                       </ItemTemplate>
                       <FooterTemplate>

<asp:TextBox ID="Item_idTextbox" Runat="server"></asp:TextBox>

</FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                 <ItemTemplate>
                               <asp:Label ID="lblitemname" runat="server" ><%# Eval("Item_name") %></asp:Label>
                       </ItemTemplate>
                        <FooterTemplate>
                                <asp:TextBox ID="txtitemname" runat="server"></asp:TextBox>
                        </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                 <ItemTemplate>
                               <asp:Label ID="lblprice" runat="server" ><%# Eval("Price") %></asp:Label>
                       </ItemTemplate>
                        <FooterTemplate>
                                <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
                        </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField >
                 <ItemTemplate>
                               <asp:Label ID="lblquantity" runat="server" ><%# Eval("Quantity") %></asp:Label>
                       </ItemTemplate>
                        <FooterTemplate>
                                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField>
               <FooterTemplate>
                   <asp:Button ID="btnAdd" runat="server" Text="Insert"  OnClick="Button1_Click"/></FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>

    </div>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:AccountConnectionString %>"
        SelectCommand="SELECT [Item_id], [Item_name], [Price], [Quantity] FROM [Item_list]"
        InsertCommand="INSERT INTO [Item_list]([Item_id], [Item_name], [Price]) ([Quantity], [Item_name], [Price]) VALUES (@Item_id, @Item_name, @Price)">
        <InsertParameters>

<asp:Parameter Type="String" Name="Item_id"></asp:Parameter>

<asp:Parameter Type="String" Name="Item_name"></asp:Parameter>

<asp:Parameter Type="String" Name="Price"></asp:Parameter>

<asp:Parameter Type="String" Name="Quantity"></asp:Parameter>

</InsertParameters>

    </asp:SqlDataSource>

    </form>
</body>
</html>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900