<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication1.Index"%>

 <!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=">
             <Columns>
                 <asp:TemplateField HeaderText="全选" >
                     <HeaderTemplate>
                         <asp:CheckBox ID="CheckBox1" runat="server" Text="全选" onclick="checkAll(this)" />
                     </HeaderTemplate>
                     <ItemTemplate>
                         <asp:CheckBox ID="CheckBox2" runat="server" />
                         <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("stuID") %>' />
                     </ItemTemplate>
                 </asp:TemplateField>
                 <asp:BoundField DataField="stuName" HeaderText="姓名" />
                 <asp:BoundField DataField="stuNo" HeaderText="学号" />
                 <asp:BoundField DataField="sex" HeaderText="性别" />
                 <asp:TemplateField HeaderText="操作">
                     <ItemTemplate>
                         <asp:Button ID="Button2" runat="server" Text="编辑" CommandArgument='<%# Eval("stuID") %>' OnClick="Button2_Click" />
                         <asp:Button ID="Button3" runat="server" Text="删除" CommandArgument='<%# Eval("stuID") %>' OnClick="Button3_Click" />
                     </ItemTemplate>
                 </asp:TemplateField>
             </Columns>
             <FooterStyle BackColor="White" ForeColor="#000066" />
             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
             <PagerTemplate>
                 <asp:Button ID="Button1" runat="server" Text="Button" />
             </PagerTemplate>
             <RowStyle ForeColor="#000066" />
             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
             <SortedAscendingCellStyle BackColor="#F1F1F1" />
             <SortedAscendingHeaderStyle BackColor="#007DBB" />
             <SortedDescendingCellStyle BackColor="#CAC9C9" />
             <SortedDescendingHeaderStyle BackColor="#00547E" />
         </asp:GridView>
         <asp:Button ID="Button4" runat="server" Text="新增" OnClick="Button4_Click" />
     </div>
     </form>
 </body>
 </html>
 <script type="text/javascript">
     function checkAll(v){
         var arr = document.getElementById("GridView1").getElementsByTagName("input");
         ; i < arr.length; i++) {
             if (arr[i].type == "checkbox")
             {
                 arr[i].checked = v.checked;
             }
         }

     }
 </script>

Index页面

 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Text;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;

 namespace WebApplication1
 {
     public partial class Index : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {
             BindeList();
         }

         private void BindeList()//第一次加载
         {
             if (!Page.IsPostBack)
             {
                 Bindlist2();
             }
         }

         private void Bindlist2()
         {
             string sql = "select * from student";
             string strcon = "server=DESKTOP-QQGOIKH;database=stuDB;uid=sa;pwd=123";
             SqlConnection sqlcon = new SqlConnection(strcon);
             //创建适配器实例,在dataset与数据之间架起桥梁作用
             SqlDataAdapter myda = new SqlDataAdapter(sql, sqlcon);
             //创建dataset实例数据集
             DataSet myds = new DataSet();
             //打开数据库
             sqlcon.Open();
             //将表填充到数据集
             myda.Fill(myds, "staff");
             GridView1.DataSource = myds;
             GridView1.DataKeyNames = new string[] { "stuID" };
             GridView1.DataBind();
         }

         //编辑
         protected void Button2_Click(object sender, EventArgs e)
         {

                 //sender代表button内容
                 Button bt = sender as Button;//尝试sender转换为button类型,如果无法转换,将返回一个空值
                 string id = bt.CommandArgument;
                 Response.Redirect("updateLinst.aspx?id=" + id);

         }
         //删除
         protected void Button3_Click(object sender, EventArgs e)
         {
             //多项删除
             ;//计数器
             //遍历griview
             foreach (GridViewRow item in GridView1.Rows)//foreach不能直接遍历gridview,只能遍历行的集合
             {
                 CheckBox cb = item.Cells[].FindControl("CheckBox2") as CheckBox;//sender as CheckBox错误
                 //获取每一行第一列的服务器控件为checkBo2的内容
                 if (cb.Checked)
                 {
                     HiddenField hf = item.Cells[].FindControl("HiddenField1") as HiddenField;
                     string id = hf.Value;
                     string sql = "delete from student where stuID=@a";
                     SqlParameter pms = new SqlParameter("@a", id);
                     SqlConnection conn = new SqlConnection("server=DESKTOP-QQGOIKH;database=stuDB;uid=sa;pwd=123");
                     conn.Open();
                     SqlCommand cmd = new SqlCommand(sql, conn);
                     cmd.Parameters.Add(pms);
                     int i = cmd.ExecuteNonQuery();
                     count += i;
                     conn.Close();
                 }
             }
             )
             {
                 Response.Write("一共删除了"+count+"条数据");
                 Bindlist2();
             }
             else
             {
                 Response.Write("删除失败");
             }
         }

         protected void Button4_Click(object sender, EventArgs e)
         {
             Response.Redirect("insertList.aspx");
         }
     }
 }

Index后台

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="updateLinst.aspx.cs" Inherits="WebApplication1.updateLinst" %>

 <!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:Label ID="Label1" runat="server" Text="姓名"></asp:Label>
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
         <br />
         <asp:Label ID="Label2" runat="server" Text="编号"></asp:Label>
         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
         <br />
         <asp:Label ID="Label3" runat="server" Text="学号"></asp:Label>
         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
         <br />
         <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
         <asp:Button ID="Button2" runat="server" Text="返回" OnClick="Button2_Click" />
     </div>
     </form>
 </body>
 </html>

修改页面

 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;

 namespace WebApplication1
 {
     public partial class updateLinst : System.Web.UI.Page
     {
         string strcon = "server=DESKTOP-QQGOIKH;database=stuDB;uid=sa;pwd=123";
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!Page.IsPostBack)
             {
                 string id = Request["id"].ToString();
                 //取stuID对应的数据
                 string sql = "select * from student where stuID=@a";
                 SqlParameter pms = new SqlParameter("@a", id);
                 //ADO.NET操作
                 SqlConnection conn = new SqlConnection(strcon);
                 conn.Open();
                 SqlCommand cmd = new SqlCommand(sql, conn);
                 //传入参数
                 cmd.Parameters.Add(pms);
                 //执行
                 SqlDataReader sdr = cmd.ExecuteReader();
                 //读取
                 bool b = sdr.Read();//b代表是否读取到数据
                 if (b==true)
                 {
                     TextBox1.Text = sdr["stuName"].ToString();
                     TextBox2.Text = sdr["stuNo"].ToString();
                     TextBox3.Text = sdr["sex"].ToString();
                 }
                 conn.Close();
             }

         }
         //提交
         protected void Button1_Click(object sender, EventArgs e)
         {
             //取到id
             string id = Request["id"];
             string sql = "update student set stuName=@b,stuNo=@c,sex=@d where stuID=@a";

             SqlParameter[] pms = ];
             pms[] = new SqlParameter("@a", id);
             pms[] = new SqlParameter("@b", TextBox1.Text);
             pms[] = new SqlParameter("@c", TextBox2.Text);
             pms[] = new SqlParameter("@d", TextBox3.Text);
             //ADO.NET操作
             SqlConnection conn = new SqlConnection(strcon);
             conn.Open();
             SqlCommand cmd = new SqlCommand(sql, conn);
             cmd.Parameters.Add(pms[]);
             cmd.Parameters.Add(pms[]);
             cmd.Parameters.Add(pms[]);
             cmd.Parameters.Add(pms[]);
             int i = cmd.ExecuteNonQuery();
             conn.Close();
             )
             {
                 Response.Write("修改成功");
             }
             else
             {
                 Response.Write("修改失败");
             }
         }
         //返回
         protected void Button2_Click(object sender, EventArgs e)
         {
             Response.Redirect("Index.aspx");
         }
     }
 }

修改后台

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="insertList.aspx.cs" Inherits="WebApplication1.insertList" %>

 <!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:Label ID="Label1" runat="server" Text="姓名"></asp:Label>
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
          <br />
          <asp:Label ID="Label2" runat="server" Text="学号"></asp:Label>
         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
         <br />
         <asp:Label ID="Label3" runat="server" Text="性别"></asp:Label>
         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
         <br />
         <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
     </div>
     </form>
 </body>
 </html>

新增页面

 using System;
 using System.Collections.Generic;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;

 namespace WebApplication1
 {
     public partial class insertList : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {

         }
         //新增提交
         protected void Button1_Click(object sender, EventArgs e)
         {

             string sql = "insert into student (stuName, stuNo,sex) values (@a, @b,@c)";
             string strcon = "server=DESKTOP-QQGOIKH;database=stuDB;uid=sa;pwd=123";
             SqlParameter[] pms = ];
             pms[] = new SqlParameter("@a", TextBox1.Text);
             pms[] = new SqlParameter("@b", TextBox2.Text);
             pms[] = new SqlParameter("@c", TextBox3.Text);
             //ADO.NET操作
             SqlConnection conn = new SqlConnection(strcon);
             conn.Open();
             SqlCommand cmd = new SqlCommand(sql, conn);
             cmd.Parameters.Add(pms[]);
             cmd.Parameters.Add(pms[]);
             cmd.Parameters.Add(pms[]);
             int i = cmd.ExecuteNonQuery();
             conn.Close();
             )
             {
                 Response.Write("添加成功");
                 Response.Redirect("index.aspx");
             }
             else
             {
                 Response.Write("添加失败");
             }
         }
     }
 }

新增后台

GridView数据绑定的更多相关文章

  1. GridView数据绑定控件的模版列时设置显示的格式

    形式 语法 结果 数字 {0:N2} 12.36   数字 {0:N0} 13   货币 {0:c2} $12.36   货币 {0:c4} $12.3656   货币 "¥{0:N2}&q ...

  2. Android GridView数据绑定

    java代码构造个泛型数组用于存放item,作为title        List<Map<String, Object>> items = new ArrayList< ...

  3. GridView--scroolview嵌套listview和gridview

    我们在真实项目中通常会遇到ListView或者GridView嵌套在ScrollView中问题.但是做的时候会发现,一旦两者进行嵌套,即会发生冲突.得不到我们希望的效果.由于ListView和Grid ...

  4. GridView 编辑修改

    //点击gridview控件自带的编辑按钮时执行的事件    protected void gvNewsList_RowEditing(object sender, GridViewEditEvent ...

  5. Windows 8实例教程系列 - 数据绑定高级实例

    原文:Windows 8实例教程系列 - 数据绑定高级实例 上篇Windows 8实例教程系列 - 数据绑定基础实例中,介绍Windows 8应用开发数据绑定基础,其中包括一些简单的数据绑定控件的使用 ...

  6. 如何让Gridview在没有数据的时候显示表头(asp.net)

    原文:如何让Gridview在没有数据的时候显示表头(asp.net) 1.前言 当对GridView控件进行数据绑定时,如果绑定的记录为空,网页上就不显示GridView,造成页面部分空白,页面布局 ...

  7. Gridview标题头添加排序图片

    向gridview标题头中添加排序图片,当点击某一个头标题时,在标题中出现升序箭头向上的图片,再点击一次时降序,在标题中出现箭头向下的图片,初始页面时在标题头中并不现实任何图片. 先定义好一个grid ...

  8. Asp.net中GridView详解《转》

    ASP.NET服务器控件GridView 1         ASP.NET 服务器控件GridView使用 本教程不介绍服务器端控件的呈现,事件处理,状态等理论知识,只介绍服务器端控件的使用操作,如 ...

  9. Gridview各种功能+AspNetPager+Ajax实现无刷新存储过程分页 (留着用)

    存储过程: GetProductsCount1: GetProductsByPage: ) * @PageSize) +' id from test)' exec sp_executesql @sql ...

随机推荐

  1. FIFO简记

    FIFO( First In First Out)简单说就是指先进先出的存储方式,在高速外设读取时经常使用. FIFO从读写的时钟可分为同步和异步两种,从信号来说都有读.写使能信号,读.写数据总线,满 ...

  2. WINDOWS遠程常規化後再次切換回到全屏

    其实有更好的方法的,CTRI+Alt+Break,博主试试看

  3. postgresql修炼之道学习笔记(1)

    好好学习吧. 本笔记 仅作为摘要记录 前两章,主要是数据库对比和安装等. 对比,就比多说了,总是和别人比较,会显得自己身价低,呵呵. 安装也有很多文章,不多说. 第二章提到了一些简单的配置, 其在 d ...

  4. H5音频和视频

    使用媒体标签最大的缺点在于缺少编解码器的支持一.浏览器支持性检测检测浏览器是否支持audio元素或video元素最简单的方式就是用脚本动态创建它,然后检测特定函数是否存在 var hasVideo=! ...

  5. Leetcode: Design Phone Directory

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  6. 【转】mybatis实战教程(mybatis in action),mybatis入门到精通

    MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis ...

  7. 论AVL树与红黑树

    首先讲解一下AVL树: 例如,我们要输入这样一串数字,10,9,8,7,15,20这样一串数字来建立AVL树 1,首先输入10,得到一个根结点10 2,然后输入9, 得到10这个根结点一个左孩子结点9 ...

  8. 浅谈php中使用websocket

    在PHP中,开发者需要考虑的东西比较多,从socket的连接.建立.绑定.监听等都需要开发者自己去操作完成,对于初学者来说,难度方面也挺大的,所以本文的思路如下: 1.socket协议的简介 2.介绍 ...

  9. Cef 架构

    cef支持各种语言和多种操作系统.在设计的时候充分考虑了性能和易用性.cef核心功能提供了c和c++的接口.cef提供了和主程序之间的通信能力(利用 custom plugins, protocols ...

  10. JNI 翻译 转 Delphi 的 经验 方法

    首发在 ①FireMonkey[移动开发] 16523232 欢迎使用 FMX 开发手机程序的高手来访. 注意:如果您看了本文,翻译了 JNI,请发布到本群共享一份.不同意本规定的,请立即删除本文.凡 ...