数据库:Northwind 下面的操作将实现下图中的功能
这是一个非常常见的数据维护页面,在asp.net1.1中要实现以上操作,还是有一定的编码工作量,如果数据底层封装得比较好,或是使用了ORM,那么这个页面在1.1下估计是2个小时完成,而使用asp.net 2.0,10分钟搞定。 1. 新建一个SqlDataSource ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
2. 查询区域 Query By CategoryName:
<asp:TextBox ID="TextBoxCategoryName" runat="server"></asp:TextBox> <asp:Button ID="ButtonQuery" runat="server" Text="Query" />
3. 添加GridView <asp:GridView ID="GridView1" runat="server" Width="90%" AutoGenerateColumns="False" DataKeyNames="CategoryID" AllowPaging="true" PageSize="10"
DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None"> <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" /> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" /> <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" /> </Columns> </asp:GridView>
4.添加DetailsView <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="CategoryID" Width="90%"
DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" HeaderText="Details"> <Fields> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName"> <ItemStyle Width="90%" /> </asp:BoundField> <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" /> </Fields> </asp:DetailsView>
5.实现选中GridView时DetailsView显示值连动操作 this.DetailsView1.PageIndex = this.GridView1.SelectedRow.DataItemIndex;
整个页面实现起来非常简单,试试看,是不是只要10分钟。
自己尝试着动手做一遍。 1、添加SqlDataSource1,配置数据为Northwind的Customers表,在图1设置SQL语句
(2)UPDATE语句:
(3)INSERT语句:
(4)DELETE语句:
2、添加Label1(Text="按公司名查询")、TextBox1(ID="CompanyName1")、 3、配置SqlDataSource1,在如图2中如图设置。
4、添加GridView1,“选择数据源”=SqlDataSource1,选择“启用分页”、“启用选定内容”。 5、添加DatailsView1,“选择数据源”=SqlDataSource1,选择“启用插入”、“启用删除”、“启用编辑”。 6、双击GridView1,添加GrideView1的SelectedIndexChanged事件处理代码:
这个是本例需要我们输入的唯一代码。 7、在HTML代码页里的<asp:SqlDataSource>...</asp:SqlDataSource>里把
改为
就不会出现调试成功却看不到东西的状况了,呵呵! 8、最后外观就在“GridView任务”的“自动套用格式”选择你想要的外观!DatailsView1的方法一样。 |
使用GridView加DetailsView实现查询,新增,编辑,删除
最新推荐文章于 2024-02-28 12:56:35 发布