看了下国外某巨牛的BLOG,大概是讲asp.net 2.0中用GRIDVIEW插入新记录的,方法比较特别,但效果一般,故将程序转之,较为简单,不做解释等。
1@ Page Language="C#" ClassName="Default_aspx"
1<script runat="server">
2void CancelButton1_Click(object sender, EventArgs e)
3{
4
5GridView1.ShowFooter = false;
6
7}
8void AddButton1_Click(object sender, EventArgs e)
9{
10
11GridView1.ShowFooter = true;
12
13}
14
15void Button1_Click(object sender, EventArgs e)
16
17{
18
19TextBox customerID = GridView1.FooterRow.FindControl("CustomerIDTextBox") as TextBox;
20
21TextBox companyName = GridView1.FooterRow.FindControl("CompanyNameTextBox") as TextBox;
22
23DropDownList ContactTitle = GridView1.FooterRow.FindControl("ContactTitleDropDownList") as DropDownList;
24
25SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text;
26
27SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text;
28
29SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue = ContactTitle.SelectedValue;
30
31SqlDataSource1.Insert();
32
33}
34
35
36</script>
1<html xmlns=" http://www.w3.org/1999/xhtml ">
2<head id="Head1" runat="server">
3<title>Untitled Page</title>
4</head>
5<body>
6<form id="form1" runat="server">
7<div>
8<asp:button id="AddButton1" onclick="AddButton1_Click" runat="Server" text="Add new Item"></asp:button>
9<asp:gridview autogeneratecolumns="False" datakeynames="CustomerID" datasourceid="SqlDataSource1" id="GridView1" runat="server" showfooter="True">
10<columns>
11<asp:templatefield>
12<itemtemplate>
13<asp:label id="CustomerIDLabel" runat="Server">```
14# Eval("CustomerID")
15```</asp:label>
16</itemtemplate>
17<footertemplate>
18<asp:textbox id="CustomerIDTextBox" runat="server"></asp:textbox>
19</footertemplate>
20</asp:templatefield>
21<asp:templatefield>
22<itemtemplate>
23<asp:label id="CompanyNameLabel" runat="Server">```
24# Eval("CompanyName")
25```</asp:label>
26</itemtemplate>
27<footertemplate>
28<asp:textbox id="CompanyNameTextBox" runat="server"></asp:textbox>
29</footertemplate>
30</asp:templatefield>
31<asp:templatefield>
32<footertemplate>
33<asp:dropdownlist datasourceid="SqlDataSource2" datatextfield="ContactTitle" datavaluefield="ContactTitle" id="ContactTitleDropDownList" runat="server">
34</asp:dropdownlist>
35<asp:sqldatasource connectionstring="server=localhost;uid=sa;password=xxx;database=northwind" id="SqlDataSource2" runat="server" selectcommand="SELECT DISTINCT [ContactTitle] FROM [Customers]">
36</asp:sqldatasource>
37<asp:button id="Button1" onclick="Button1_Click" runat="server" text="Add"></asp:button>
38<asp:button id="CancelButton1" onclick="CancelButton1_Click" runat="server" text="Cancel"></asp:button>
39</footertemplate>
40<itemtemplate>
41<asp:dropdownlist datasourceid="SqlDataSource3" datatextfield="ContactTitle" datavaluefield="ContactTitle" id="ContactTitleDropDown" runat="Server" selectedvalue='```
42# Bind("ContactTitle")
43```'></asp:dropdownlist>
44<asp:sqldatasource connectionstring="server=localhost;uid=sa;password=xxx;database=northwind" enablecaching="True" id="SqlDataSource3" runat="server" selectcommand="SELECT DISTINCT [ContactTitle] FROM [Customers]">
45</asp:sqldatasource>
46</itemtemplate>
47</asp:templatefield>
48</columns>
49</asp:gridview>
50<asp:sqldatasource connectionstring="server=localhost;uid=sa;password=xxxxx;database=northwind" id="SqlDataSource1" insertcommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)" runat="server" selectcommand="SELECT [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]">
51<deleteparameters>
52<asp:parameter name="CustomerID" type="String"></asp:parameter>
53</deleteparameters>
54<updateparameters>
55<asp:parameter name="CompanyName" type="String"></asp:parameter>
56<asp:parameter name="ContactTitle" type="String"></asp:parameter>
57<asp:parameter name="CustomerID" type="String"></asp:parameter>
58</updateparameters>
59<insertparameters>
60<asp:parameter name="CustomerID" type="String"></asp:parameter>
61<asp:parameter name="CompanyName" type="String"></asp:parameter>
62<asp:parameter name="ContactTitle" type="String"></asp:parameter>
63</insertparameters>
64</asp:sqldatasource>
65</div>
66</form>
67</body>
68</html>