新建数据,采用sql server数据库
use dflx;
create table person --建立表
(
name char(12),
id int primary key,
age int,
sf char(16) default 'worker'
)
drop table person; --删除表格。
insert into person values('张三',110,25,'manager');
insert into person values('李四',111,25,'manager');
insert into person values('王五',101,26,default);
delete from person where id=101; --删除差错的数据
select * from person;
结果如下
aspx文件采用控件拖拉的,没有写代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="User.aspx.cs" Inherits="User" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dflxConnectionString %>" DeleteCommand="DELETE FROM [person] WHERE [id] = @id" InsertCommand="INSERT INTO [person] ([name], [id], [age], [sf]) VALUES (@name, @id, @age, @sf)" SelectCommand="SELECT * FROM [person]" UpdateCommand="UPDATE [person] SET [name] = @name, [age] = @age, [sf] = @sf WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="age" Type="Int32" />
<asp:Parameter Name="sf" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="age" Type="Int32" />
<asp:Parameter Name="sf" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:BoundField DataField="sf" HeaderText="sf" SortExpression="sf" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:dflxConnectionString %>" DeleteCommand="DELETE FROM [person] WHERE [id] = @id" InsertCommand="INSERT INTO [person] ([name], [id], [age], [sf]) VALUES (@name, @id, @age, @sf)" SelectCommand="SELECT * FROM [person] WHERE ([id] = @id)" UpdateCommand="UPDATE [person] SET [name] = @name, [age] = @age, [sf] = @sf WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="age" Type="Int32" />
<asp:Parameter Name="sf" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="age" Type="Int32" />
<asp:Parameter Name="sf" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource2" Height="50px" OnItemUpdated="DetailsView1_ItemUpdated" Width="351px" OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted">
<Fields>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:BoundField DataField="sf" HeaderText="sf" SortExpression="sf" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<br />
<br />
<br />
<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource2" Height="50px" Width="277px">
<Fields>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:TemplateField HeaderText="sf" SortExpression="sf">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>worker</asp:ListItem>
<asp:ListItem>manager</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("sf") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("sf") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<br />
</div>
</form>
</body>
</html>
c#文件就写了一点东西,控件是一个好东西(捂嘴笑)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class User : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
GridView1.DataBind();
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
}
protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
GridView1.DataBind();
}
}
效果如图所示
先把数据库文件,引入到vs中,实现了网页对表格的增删改查的相关操作
相关的更多知识点文章(方便自己他人查看)
数据绑定(数据源控件 -- SqlDataSource)
使用SqlDataSource插入、更新以及删除数据
asp.net学习之SqlDataSource
GridView
DetailsView
DetailsView的使用