初学ASP.NET不久自己动手做了个GridView简单的例子(感谢爱追逐的小猪、等等网络朋友)
VS2005下调试成功(个人感觉不错 遗憾的是界面不美观)。
主要目的是学习GridView控件。
表结构如下:
--创建student表
create table student
(
stuID int identity(1,1) primary key, --学号(只是为了方便)
stuName varchar(10) not null, --姓名
stuSex varchar(2) not null, --性别
stuAge int , --年龄
stuAddress varchar(200) --家庭地址
)
default2.aspx前台代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView使用例子</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" DataKeyNames="stuID" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
EmptyDataText="没有记录"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating"
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound"
EditRowStyle-BackColor="#ebebeb">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text=" " Width="24px" Font-Size="10" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="stuID" HeaderText="学号" ItemStyle-Font-Size="10" ReadOnly="True"/>
<asp:BoundField DataField="stuName" HeaderText="姓名" ItemStyle-Font-Size="10" ControlStyle-Width="50px"/>
<asp:BoundField DataField="stuAge" HeaderText="年龄" ItemStyle-Font-Size="10" ControlStyle-Width ="20px" />
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="Label1" Font-Size ="10" runat="server" Text='<%# Bind("stuSex", "{0}") %>' Width="56px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" Font-Size="10" runat="server" SelectedValue='<%# Bind("stuSex", "{0}") %>'>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="stuAddress" HeaderText="地址" ItemStyle-Font-Size="10" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="全部选中" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="删除所选" OnClick="Button2_Click" OnClientClick="return confirm('真的要删除选中的项目吗?');" /><br />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing; // 要使用颜色,引入命名空间
public partial class Default2 : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
...{
BindGrid();
}
}
protected void BindGrid()