这个练习,也基本是使用的可视化设计,点选的各个配置项,代码是最后的结果。
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test9_4.WebForm1" %>
<!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:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PageSize="3" Width="462px">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="365px" AllowPaging="True" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateRows="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="Black">
<EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="content" HeaderText="content" SortExpression="content" />
<asp:BoundField DataField="categories" HeaderText="categories" SortExpression="categories" />
<asp:BoundField DataField="type1" HeaderText="type1" SortExpression="type1" />
<asp:BoundField DataField="issueDate" HeaderText="issueDate" SortExpression="issueDate" />
</Fields>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db_newsConnectionString %>" SelectCommand="SELECT * FROM [tb_news]" DeleteCommand="DELETE FROM [tb_news] WHERE [id] = @id" InsertCommand="INSERT INTO [tb_news] ([title], [content], [categories], [type1], [issueDate]) VALUES (@title, @content, @categories, @type1, @issueDate)" UpdateCommand="UPDATE [tb_news] SET [title] = @title, [content] = @content, [categories] = @categories, [type1] = @type1, [issueDate] = @issueDate WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="content" Type="String" />
<asp:Parameter Name="categories" Type="String" />
<asp:Parameter Name="type1" Type="String" />
<asp:Parameter Name="issueDate" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="content" Type="String" />
<asp:Parameter Name="categories" Type="String" />
<asp:Parameter Name="type1" Type="String" />
<asp:Parameter Name="issueDate" Type="DateTime" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace test9_4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.PageIndex = GridView1.SelectedRow.DataItemIndex;
}
}
}
sql
CREATE DATABASE db_news
USE db_news
GO
CREATE TABLE dbo.tb_news(
id int identity(1,1) not null primary key,
title varchar(50) not null,
content varchar(2000) not null,
categories varchar(50) not null,
type1 varchar(50) not null,
issueDate datetime not null default getdate())
GO
insert into tb_news(title,content,categories,type1)
values('创业板开板','新浪财经讯10月17日上午消息 中国证监会主席在企业投融资论坛上透露,经国务院批准成立创业板。','环球经济','国内新闻');
web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="db_newsConnectionString" connectionString="Data Source=LAPTOP-AQKEN65V\SQLEXPRESS08;Initial Catalog=db_news;Persist Security Info=True;User ID=sa;Password=123456"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>