asp.net2.0中用GRIDVIEW插入新记录

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
看了下国外某巨牛的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>

<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">

</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:SqlDataSource 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>

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值