2012年11月-.Net学习笔记-GridView-ASP.Net-(停止更新)
2012年11月-.Net学习笔记-GridView-ASP.Net-(更新)
1.快速写封装字段(set&get):Ctrl+R,E +Enter+Enter
2.设置主键:DataKeyName = 要设为主键的字段
3.GridView绑定数据:
GridView.DataSource = 数据源;
GridView.DataBind();
设置GridView分页:
AllowPaging = "true";
PageSize = 每一页显示的行数
还要设置下面的:
protected void GridView1_PageIndexChanging(object sender,GridViewPageEvenArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.BindData();//重新绑定 自己写的方法
}
还有删除和编辑的按钮的实现:
后台代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class index : System.Web.UI.Page 9 { 10 static List<Person> personlist = new List<Person>(); 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 14 if (!IsPostBack) 15 { 16 personlist.Add(new Person("网络", "20", "199011", "12345678")); 17 personlist.Add(new Person("张山", "20", "199011", "12345678")); 18 personlist.Add(new Person("王五", "20", "199011", "12345678")); 19 personlist.Add(new Person("李四", "20", "199011", "12345678")); 20 personlist.Add(new Person("李威", "20", "199011", "12345678")); 21 personlist.Add(new Person("王倩", "20", "199011", "12345678")); 22 personlist.Add(new Person("王里", "20", "199011", "12345678")); 23 this.BindData(); 24 } 25 } 26 27 private void BindData() 28 { 29 GridView1.DataSource = personlist; 30 GridView1.DataBind(); 31 } 32 33 34 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)//当前索引发生改变前 35 { 36 GridView1.PageIndex = e.NewPageIndex; 37 this.BindData(); 38 } 39 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//GridView内激发编辑事件 40 { 41 GridView1.EditIndex = e.NewEditIndex; 42 this.BindData(); 43 } 44 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消编辑 45 { 46 GridView1.EditIndex = -1; 47 this.BindData(); 48 } 49 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//激发删除 50 { 51 int row = e.RowIndex; 52 int page = GridView1.PageIndex; 53 int count = row * (page+1); 54 personlist.RemoveAt(count); 55 this.BindData(); 56 } 57 } 58 internal class Person 59 { 60 private string name; 61 62 public string Name 63 { 64 get { return name; } 65 set { name = value; } 66 } 67 68 private string age; 69 70 public string Age 71 { 72 get { return age; } 73 set { age = value; } 74 } 75 76 private string birth; 77 78 public string Birth 79 { 80 get { return birth; } 81 set { birth = value; } 82 } 83 84 private string tel; 85 86 public string Tel 87 { 88 get { return tel; } 89 set { tel = value; } 90 } 91 92 public Person(string name, string age, string birth, string tel) 93 { 94 this.name = name; 95 this.age = age; 96 this.birth = birth; 97 this.tel = tel; 98 } 99 }
前台代码
1 <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="true" CodeFile="index.aspx.cs" Inherits="index" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 <div> 12 13 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 14 AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 15 ForeColor="#333333" GridLines="None" 16 onpageindexchanging="GridView1_PageIndexChanging" 17 onrowcancelingedit="GridView1_RowCancelingEdit" 18 onrowediting="GridView1_RowEditing" PageSize="3" 19 onrowdeleting="GridView1_RowDeleting"> 20 <AlternatingRowStyle BackColor="White" /> 21 <Columns> 22 <asp:BoundField DataField="name" HeaderText="姓名" /> 23 <asp:BoundField DataField="age" HeaderText="年龄" /> 24 <asp:BoundField DataField="birth" HeaderText="出生" /> 25 <asp:BoundField DataField="tel" HeaderText="电话" /> 26 <asp:CommandField ShowSelectButton="True" /> 27 <asp:CommandField ShowDeleteButton="True" /> 28 <asp:CommandField ShowEditButton="True" /> 29 </Columns> 30 <EditRowStyle BackColor="#2461BF" /> 31 <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 32 <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 33 <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 34 <RowStyle BackColor="#EFF3FB" /> 35 <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 36 <SortedAscendingCellStyle BackColor="#F5F7FB" /> 37 <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 38 <SortedDescendingCellStyle BackColor="#E9EBEF" /> 39 <SortedDescendingHeaderStyle BackColor="#4870BE" /> 40 </asp:GridView> 41 42 </div> 43 </form> 44 </body> 45 </html>
2.GridView无刷新分页的实现(UpdatePanel,ScriptManager);
修改的前台代码
1 <body> 2 <form id="form1" runat="server"> 3 <div> 4 <asp:ScriptManager ID="scriptmanager1" runat="server"></asp:ScriptManager> 5 <asp:UpdatePanel ID="updatepanel1" runat="server"> 6 <Triggers> 7 <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" /><!--绑定分页事件--> 8 <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowEditing" /><!--绑定编辑事件--> 9 <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowDeleting" /><!--绑定删除事件--> 10 <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCancelingEdit" /><!--绑定删除事件--> 11 </Triggers> 12 <ContentTemplate> 13 <asp:GridView ID="GridView1" runat="server" AllowPaging="True"


浙公网安备 33010602011771号