GridView实现编辑,选择删除,分页功能

这篇博客介绍了如何在ASP.NET环境下使用GridView控件实现数据编辑、选择和删除功能,以及分页操作。作者通过创建student表并展示如何在前台代码中设置GridView属性,包括添加模板字段、绑定数据字段以及设置编辑、取消、更新事件。此外,还展示了如何添加全选和删除按钮,并在客户端添加确认提示。
摘要由CSDN通过智能技术生成

初学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>
        &nbsp;&nbsp;
        <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>

 

 

 

default2.aspx.cs后台代码如下:
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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值