网格视图项目模板字段自动更改 [英] grid view item template field change automatically

查看:59
本文介绍了网格视图项目模板字段自动更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网格视图中使用了模板字段,但是当我将sqldatasource附加到它时,所有这些字段都变为有界字段,我代码的基本动机是通过网格视图将数据插入数据库中.

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

<%@ 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>

推荐答案

ConnectionStrings:AccountConnectionString %> " SelectCommand =" 从[Item_list]中选择[Item_id],[Item_name],[Price],[Quantity]" span> InsertCommand =" > < InsertParameters > < asp:Parameter 类型 =" 名称 Item_id " < > < asp:Parameter 类型 =" 名称 Item_name " < > < asp:Parameter 类型 =" 名称 价格" < > < asp:Parameter 类型 =" 名称 数量" < > < /InsertParameters > < /asp:SqlDataSource > < /form > < /body > < /html >
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>


这篇关于网格视图项目模板字段自动更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆