ASP.NET的Gridview控件简单使用(分页、序号、选框、全选)(附源码)
来源:互联网 发布:帆布包推荐 知乎 编辑:程序博客网 时间:2024/06/06 07:15
这次我们实现的功能是:全选,单选,可以提取一行的数据,分页,结合下拉列表筛选数据,显示序号。
1.全选
//全选
protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl(“CheckBox1”);
if (CheckBox2.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
}
当点击全选框的时候,其它框也会被选中
注:CheckBox的AutoPostBack属性 要 设置 为 True
2.单选提取一行中的数据
//提交按钮
protected void Button1_Click1(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl(“CheckBox1”);
if (cbox.Checked == true)
{
string userid = GridView1.DataKeys[i].Value.ToString();
DB db = new DB();
string sqlstr = “select username from base_user where userid = ‘” + userid + “’ and auditcode!=’3’ and auditcode!=’0’”;
int cot = db.reDt(sqlstr).Rows.Count;
if (cot > 0)
{
string name = db.reDt(sqlstr).Rows[0][“username”].ToString();
DataInterface di = new DataInterface();
string ss = di.synResInfo(“11201”, “440000”, userid, name);
//Response.Write(ss);
string time = DateTime.Now.ToLongDateString().ToString();
string Respondlog = “log\” + time + “Respond.xml”;
}
//Response.Redirect(Respondlog);
}
}
int cp = Convert.ToInt32(DropDownList1.Text);
this.bind(cp);
}
3.显示序号
< asp:GridView ID=”GridView1” runat=”server” AutoGenerateColumns=”False” style=” width:100%;”>
< Columns>
< asp:TemplateField HeaderStyle-Width=”10%”>
< ItemTemplate>
<%# (Container.DataItemIndex + 1) + (Convert.ToInt32(this.Label6.Text) - 1) * (Convert.ToInt32(DropDownList1.Text))%>
< /ItemTemplate>
< /asp:TemplateField>
< asp:TemplateField>
< ItemTemplate>
< asp:CheckBox ID=”CheckBox1” runat=”server” />
< /ItemTemplate>
< /asp:TemplateField>
< /Columns>
< SelectedRowStyle BackColor=”#669999” Font-Bold=”True” ForeColor=”White” />
< /asp:GridView>
解释:<%# (Container.DataItemIndex + 1) + (Convert.ToInt32(this.Label6.Text) - 1) * (Convert.ToInt32(DropDownList1.Text))%>
Container.DataItemIndex + 1 :为当页的数量
this.Label6.Text:总页数
DropDownList1.Text:每页显示数
4.前端源码
< form id="form1" runat="server" method="post" action=""><center> <div id="topNav"> <table style="width: 1000px; height: 36px; border-right-width: 2px; border-left-width: 2px; border-bottom-width: 2px; border-top-width: 2px; margin-bottom: 15px; margin-top: 10px;"> <tr> <td style="width: 10px; padding-top: 5px;"> </td> <td style="width: 100px; padding-top: 5px;"> <div class="button-group"> <asp:Button ID="Button1" class="button bg-main" runat="server" Text="提交上报" OnClick="Button1_Click1" /> </div> </td> <td> <div class="button-group"> <asp:Button ID="Button2" class="button bg-main" runat="server" Text="退回信息" OnClick="Button2_Click1" /> </div> </td> <td> <asp:CheckBox ID="CheckBox2" runat="server" Font-Size="9pt" Text="全选" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged1" /> 学员信息状态:<asp:DropDownList ID="sh_status" runat="server" AutoPostBack="True" OnSelectedIndexChanged="sh_status_SelectedIndexChanged"> <asp:ListItem Value="1">全部</asp:ListItem> <asp:ListItem Value="2">等待上报</asp:ListItem> <asp:ListItem Value="3">成功上报</asp:ListItem> <asp:ListItem Value="4">上报失败</asp:ListItem> <asp:ListItem Value="5">学员修改</asp:ListItem> </asp:DropDownList> </td> <td style="width: 350px; padding-top: 5px;"> </td> </tr> </table> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style=" width:100%;"> <Columns> <asp:TemplateField HeaderStyle-Width="10%"> <ItemTemplate> <%# (Container.DataItemIndex + 1) + (Convert.ToInt32(this.Label6.Text) - 1) * (Convert.ToInt32(DropDownList1.Text))%> </ItemTemplate> <HeaderStyle Width="50px" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> <HeaderStyle Width="50px" /> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> <asp:BoundField DataField="username" HeaderText="姓名"> <HeaderStyle Width="100px" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="userid" HeaderText="身份证件号"> <FooterStyle Width="200px" /> <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="auditcode" HeaderText="学员信息状态"> <HeaderStyle Width="150px" HorizontalAlign="Center" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="reported_status" HeaderText="备注"> <HeaderStyle Width="350px" HorizontalAlign="Center" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:BoundField> </Columns> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> </asp:GridView> <div id="bottomNav"> <table style="width: 1000px; height: 36px; border-right-width: 2px; border-left-width: 2px; border-bottom-width: 2px; border-top-width: 2px; margin-bottom: 0px; margin-top: 15px;"> <tr> <td style="width: 400px; padding-top: 5px;"> <asp:LinkButton ID="lnkbtnOne" runat="server" OnClick="lnkbtnOne_Click">首页</asp:LinkButton> <asp:LinkButton ID="lnkbtnUp" runat="server" OnClick="lnkbtnUp_Click">上一页</asp:LinkButton> 第<asp:Label ID="Label6" runat="server" Text="1"></asp:Label>页 共<asp:Label ID="Label7" runat="server" Text="1"></asp:Label>页 <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnBack" runat="server" OnClick="lnkbtnBack_Click">尾页</asp:LinkButton> </td> <td style="width: 400px; padding-top: 5px;"> </td> <td> </td> <td> </td> <td> 共<asp:Label ID="Lall" runat="server" Text=""></asp:Label>条, 每页<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="50px" AutoPostBack="True"> <asp:ListItem Value="50">50</asp:ListItem> <asp:ListItem Value="100">100</asp:ListItem> <asp:ListItem Value="200">200</asp:ListItem> <asp:ListItem Value="300">300</asp:ListItem> <asp:ListItem Value="500">500</asp:ListItem> <asp:ListItem Value="1000">1000</asp:ListItem> </asp:DropDownList> 条 </td> </tr> </table> </div></center><br><br></form>
注:这里只贴出from里面的代码
5.后台cs文件源码
protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { bind(Convert.ToInt32(DropDownList1.Text)); }}public void bind(int cp){ //上报状态 string auditcode = sh_status.Text; string ac = ""; if (auditcode.Equals("1")) { ac = "auditcode != '0' and auditcode!='1' or dateupload='2'"; } else if (auditcode.Equals("2")) { ac = "auditcode = '2'"; } else if (auditcode.Equals("3")) { ac = "auditcode = '3'"; } else if (auditcode.Equals("4")) { ac = "auditcode = '4'"; } else if (auditcode.Equals("5")) { ac = "auditcode!='4' and dateupload='2'"; } SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=guipei;Persist Security Info=True;User ID=zh;Password=sa"); SqlDataAdapter da = new SqlDataAdapter("select username,userid,reported_status,(case auditcode when '2' then '等待上报' when '3' then '成功上报' when '4' then '上报失败!' when '0' then '学员修改中' when '1' then '学员修改中' end) as auditcode from base_user where " + ac, con); DataSet ds = new DataSet(); da.Fill(ds, "base_user"); /*PagedDataSource ps = new PagedDataSource(); ps.DataSource = ds.Tables["base_user"].DefaultView; ps.AllowPaging = true; ps.PageSize = 2; ps.CurrentPageIndex = 0;*/ /*this.DataList1.DataSource = ps; this.DataList1.DataKeyField = "SID"; this.DataList1.DataBind();*/ /*DataList1.DataSource = ps; DataList1.DataKeyField = "userid"; DataList1.DataBind();*/ int curpage = Convert.ToInt32(this.Label6.Text); PagedDataSource ps = new PagedDataSource(); ps.DataSource = ds.Tables["base_user"].DefaultView; int all = ps.Count; Lall.Text = all.ToString(); ps.AllowPaging = true; //是否可以分页 ps.PageSize = cp; //显示的数量 ps.CurrentPageIndex = curpage - 1; //取得当前页的页码 this.lnkbtnUp.Enabled = true; this.lnkbtnNext.Enabled = true; this.lnkbtnBack.Enabled = true; this.lnkbtnOne.Enabled = true; if (curpage == 1) { this.lnkbtnOne.Enabled = false;//不显示第一页按钮 this.lnkbtnUp.Enabled = false;//不显示上一页按钮 } if (curpage == ps.PageCount) { this.lnkbtnNext.Enabled = false;//不显示下一页 this.lnkbtnBack.Enabled = false;//不显示最后一页 } this.Label7.Text = Convert.ToString(ps.PageCount); this.GridView1.DataSource = ps; this.GridView1.DataKeyNames = new string[] { "userid" };//.DataKeyField = "userid"; this.GridView1.DataBind();}protected void lnkbtnOne_Click(object sender, EventArgs e){ this.Label6.Text = "1"; this.bind(Convert.ToInt32(DropDownList1.Text));}protected void lnkbtnUp_Click(object sender, EventArgs e){ this.Label6.Text = Convert.ToString(Convert.ToInt32(this.Label6.Text) - 1); this.bind(Convert.ToInt32(DropDownList1.Text));}protected void lnkbtnNext_Click(object sender, EventArgs e){ this.Label6.Text = Convert.ToString(Convert.ToInt32(this.Label6.Text) + 1); this.bind(Convert.ToInt32(DropDownList1.Text));}protected void lnkbtnBack_Click(object sender, EventArgs e){ this.Label6.Text = this.Label7.Text; this.bind(Convert.ToInt32(DropDownList1.Text));}//分页结束//每页显示多少条protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){ Label6.Text = "1"; int cp = Convert.ToInt32(DropDownList1.Text); this.bind(cp);}//选择显示的提交状态protected void sh_status_SelectedIndexChanged(object sender, EventArgs e){ Label6.Text = "1"; int cp = Convert.ToInt32(DropDownList1.Text); this.bind(cp);}
注:这里不再贴出上述的源码
- ASP.NET的Gridview控件简单使用(分页、序号、选框、全选)(附源码)
- 简单的自定义分页控件实现(Asp.net)
- gridView (分页)(asp.net)
- [Asp.Net]GridView序号
- asp.net控件的分页批量全选删除
- asp.net中的Gridview控件添加序号列
- [原]ASP.NET(C#)Repeater分页控件(含源码)
- asp.net 中gridview控件分页的实现
- asp.net的分页代码 (不是分页控件)
- asp.net 分页控件AspNetPager的使用
- 基于.NET CompactFramework的九宫格控件(附源码)
- WPF or Silverligth使用WGrid和ScrollViewer自定义GridView控件(附源码)
- asp.net 中GridView控件实现全选及反选的功能
- asp.net 中GridView控件实现全选及反选的功能
- asp.net 中GridView控件实现全选及反选的功能
- asp.net 中GridView控件实现全选及反选的功能
- 使用Asp.net 2.0设计的三种控件(TreeView,DropDownList,GridView)的无限分类
- ASP.NET使用GridView控件、DetailsView控件
- 电子老鼠闯迷宫 SSL1455
- Java数据结构和算法-链表(4-双向链表)
- Linux下安装tar.gz类型的jdk,并配置环境变量
- 〖随记〗简单生活,开心就好~
- git添加忽略名单
- ASP.NET的Gridview控件简单使用(分页、序号、选框、全选)(附源码)
- 算法篇-8-回溯法-N皇后&最优装载&01背包
- 1006. Sign In and Sign Out (25)(-selfdone)
- Oracle触发器用法实例详解
- aop实现原理(自学)
- React开发环境配置
- zookeeper特性
- pva-faster-rcnn
- linux shell 算数运算.关系运算.布尔运算,字符串运算