程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> asp.net 2.0中用GRIDVIEW插入新記錄

asp.net 2.0中用GRIDVIEW插入新記錄

編輯:.NET實例教程

看了下國外某巨牛的BLOG,大概是講ASP.Net 2.0中用GRIDVIEW插入新記錄的,方法比較特別,但效果一般,故將程序轉之,較為簡單,不做解釋等。
<%@ Page Language="C#" ClassName="Default_ASPx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
    void CancelButton1_Click(object sender, EventArgs e)
    {

        GridVIEw1.ShowFooter = false;

    }
    void AddButton1_Click(object sender, EventArgs e)
    {

        GridVIEw1.ShowFooter = true;

    }

    void Button1_Click(object sender, EventArgs e)

    {
        
        TextBox customerID = GridVIEw1.FooterRow.FindControl("CustomerIDTextBox") as TextBox;

        TextBox companyName = GridVIEw1.FooterRow.FindControl("CompanyNameTextBox") as TextBox;

        DropDownList ContactTitle = GridVIEw1.FooterRow.FindControl("ContactTitleDropDownList") as DropDownList;

        SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text;

        SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text;

        SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue = ContactTitle.SelectedValue;

        SqlDataSource1.Insert();

    }
 

</script>

<Html XMLns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>
    <ASP:Button ID="AddButton1" runat="Server" Text="Add new Item" OnClick="AddButton1_Click" />


    <ASP:GridView ID="GridVIEw1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID"

            AutoGenerateColumns="False" ShowFooter="True">

            <Columns>

                <ASP:TemplateFIEld>

                    <ItemTemplate>

&nb
sp;                       <asp:Label ID="CustomerIDLabel" Runat="Server"><%# Eval("CustomerID") %></ASP:Label>

                    </ItemTemplate>

                    <FooterTemplate>

                        <asp:TextBox ID="CustomerIDTextBox" Runat="server"></ASP:TextBox>

                    </FooterTemplate>

                </ASP:TemplateFIEld>

                <ASP:TemplateFIEld>

                    <ItemTemplate>

                        <asp:Label ID="CompanyNameLabel" Runat="Server"><%# Eval("CompanyName") %></ASP:Label>

                    </ItemTemplate>

                    <FooterTemplate>

                        <asp:TextBox ID="CompanyNameTextBox" Runat="server"></ASP:TextBox>

                    </FooterTemplate>

                </ASP:TemplateFIEld>

                <ASP:TemplateFIEld>

                    <FooterTemplate>

                        <ASP:DropDownList ID="ContactTitleDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="ContactTitle" DataValueFIEld="ContactTitle">

            &nbs
p;           </ASP:DropDownList>

                        <ASP:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"

                            ConnectionString="server=localhost;uid=sa;passWord=xxx;database=northwind">

                        </ASP:SqlDataSource>

                        <ASP:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" />
  <ASP:Button ID="CancelButton1" Runat="server" Text="Cancel" OnClick="CancelButton1_Click" />

                    </FooterTemplate>

                    <ItemTemplate>

                        <asp:DropDownList ID="ContactTitleDropDown" SelectedValue='<%# Bind("ContactTitle") %>' Runat="Server" DataSourceID="SqlDataSource3" DataTextField="ContactTitle" DataValueFIEld="ContactTitle" ></ASP:DropDownList>

                        <ASP:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"

                            ConnectionString="server=localhost;uid=sa;passWord=xxx;database=northwind" EnableCaching="True">

                        </ASP:SqlDataSource>

                    </ItemTemplate>

                </ASP:TemplateFIEld>

            </Columns>

        </ASP:GridVIEw>

        <ASP:Sq
lDataSource ID="SqlDataSource1" Runat="server"

            InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)"

            SelectCommand="SELECT [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]"

            ConnectionString="server=localhost;uid=sa;passWord=xxxxx;database=northwind">

            <DeleteParameters>

                <asp:Parameter Type="String" Name="CustomerID"></ASP:Parameter>

            </DeleteParameters>

            <UpdateParameters>

                <asp:Parameter Type="String" Name="CompanyName"></ASP:Parameter>

                <asp:Parameter Type="String" Name="ContactTitle"></ASP:Parameter>

                <asp:Parameter Type="String" Name="CustomerID"></ASP:Parameter>

            </UpdateParameters>

            <InsertParameters>

                <asp:Parameter Type="String" Name="CustomerID"></ASP:Parameter>

                <asp:Parameter Type="String" Name="CompanyName"></ASP:Parameter>

                <asp:Parameter Type="String" Name="ContactTitle"></ASP:Parameter>

            </InsertParameters>

        </ASP:SqlDataSource>

    </div>

    </form>

</body>

</Html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved