gridview控件中数据分页的代码怎么写?我在属性里设置的怎么运行时提示出错呢?请高手帮忙。万分感谢
vs2005中gridview自带的在启用分页复选框上打勾了,怎么还是不行呢?是不是其他还要加写什么代码啊?pageindexchanging事件的代码应该怎么写啊?...
vs2005中gridview 自带的 在启用分页复选框上打勾了,怎么还是不行呢?是不是其他还要加写什么代码啊?pageindexchanging事件的代码应该怎么写啊?
展开
2个回答
展开全部
你说的那个是在写好pageindexchanging事件的前提下啊,
下面是我的CS页,希望对你有帮助:
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.MaintainScrollPositionOnPostBack = true;
this.GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
this.GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
// bind();//简单的绑定
if (!IsPostBack)//有序的绑定,并且加了自动编号
{
ViewState["SortOrder"] = "GoodsPrice";//数据库中的字段
ViewState["OrderDire"] = "ASC";//按升序排列(降序DESC)
BindSort();
}
}
/// <summary>
/// 简单绑定GridView1,实现再页面中的数据的传递
/// </summary>
void bind()
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
try
{
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tb_GoodsInfo", sqlcon);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
ds.Dispose();
da.Dispose();
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
finally
{
sqlcon.Close();
}
}
/// <summary>
/// 带排序的绑定数据
/// </summary>
void BindSort()
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
try
{
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tb_GoodsInfo", sqlcon);
DataSet ds = new DataSet();
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
string ss= (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];//注意这里两者之间必须有一个空格,否则会出错误。
dv.Sort = ss;
this.GridView1.DataSource =dv;
this.GridView1.DataBind();
ds.Dispose();
da.Dispose();
//定义排序表达式
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
finally
{
sqlcon.Close();
}
}
/// <summary>
/// 行绑定事件中添加自动编号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)//头标题的索引下标默认为-1,这里为了使头标题不改变
{
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sp = e.SortExpression;//通过事件获取排序表达式
if (ViewState["SortOrder"].ToString() == sp)//判断当前的排序表达式是否和定义的一致
{
if (ViewState["OrderDire"].ToString() == "DESC")//判断当前的排序顺序是否是否为降序(如果是就变为升序),else变为降序。
{
ViewState["OrderDire"] = "ASC";
}
else
{
ViewState["OrderDire"] = "DESC";
}
}
else
{
ViewState["SortOrder"] = sp;//把当前的排序表达式赋值给ViewState["SortOrder"],以便继续进行以后的排序
}
BindSort();//重新绑定,重画界面
}
/// <summary>
/// 分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;//获得新页的索引。
BindSort();//重新绑定画界面。
}
/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
//循环数据
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
bool flag = this.CheckBox2.Checked;
if (flag)
{
cb.Checked = true;
}
else
{
cb.Checked = false;
}
}
}
}
下面是前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<table style="width: 544px">
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BorderColor="Lime" BorderStyle="Inset" BorderWidth="1px"
CellPadding="4" DataKeyNames="GoodsID" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField HeaderText="自动编号" />
<asp:BoundField DataField="GoodsName" HeaderText="商品名称" SortExpression="GoodsName" />
<asp:BoundField DataField="GoodsPrice" HeaderText="商品价格" SortExpression="GoodsPrice" />
<asp:HyperLinkField DataNavigateUrlFields="GoodsID" DataNavigateUrlFormatString="Default2.aspx?id={0}"
HeaderText="查看详细信息" Text="详细信息" />
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="选择项" ShowSelectButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged"
Text="全选" AutoPostBack="True" /></td>
<td style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
下面是我的CS页,希望对你有帮助:
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.MaintainScrollPositionOnPostBack = true;
this.GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
this.GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
// bind();//简单的绑定
if (!IsPostBack)//有序的绑定,并且加了自动编号
{
ViewState["SortOrder"] = "GoodsPrice";//数据库中的字段
ViewState["OrderDire"] = "ASC";//按升序排列(降序DESC)
BindSort();
}
}
/// <summary>
/// 简单绑定GridView1,实现再页面中的数据的传递
/// </summary>
void bind()
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
try
{
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tb_GoodsInfo", sqlcon);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
ds.Dispose();
da.Dispose();
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
finally
{
sqlcon.Close();
}
}
/// <summary>
/// 带排序的绑定数据
/// </summary>
void BindSort()
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
try
{
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tb_GoodsInfo", sqlcon);
DataSet ds = new DataSet();
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
string ss= (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];//注意这里两者之间必须有一个空格,否则会出错误。
dv.Sort = ss;
this.GridView1.DataSource =dv;
this.GridView1.DataBind();
ds.Dispose();
da.Dispose();
//定义排序表达式
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
finally
{
sqlcon.Close();
}
}
/// <summary>
/// 行绑定事件中添加自动编号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)//头标题的索引下标默认为-1,这里为了使头标题不改变
{
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sp = e.SortExpression;//通过事件获取排序表达式
if (ViewState["SortOrder"].ToString() == sp)//判断当前的排序表达式是否和定义的一致
{
if (ViewState["OrderDire"].ToString() == "DESC")//判断当前的排序顺序是否是否为降序(如果是就变为升序),else变为降序。
{
ViewState["OrderDire"] = "ASC";
}
else
{
ViewState["OrderDire"] = "DESC";
}
}
else
{
ViewState["SortOrder"] = sp;//把当前的排序表达式赋值给ViewState["SortOrder"],以便继续进行以后的排序
}
BindSort();//重新绑定,重画界面
}
/// <summary>
/// 分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;//获得新页的索引。
BindSort();//重新绑定画界面。
}
/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
//循环数据
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
bool flag = this.CheckBox2.Checked;
if (flag)
{
cb.Checked = true;
}
else
{
cb.Checked = false;
}
}
}
}
下面是前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<table style="width: 544px">
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BorderColor="Lime" BorderStyle="Inset" BorderWidth="1px"
CellPadding="4" DataKeyNames="GoodsID" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField HeaderText="自动编号" />
<asp:BoundField DataField="GoodsName" HeaderText="商品名称" SortExpression="GoodsName" />
<asp:BoundField DataField="GoodsPrice" HeaderText="商品价格" SortExpression="GoodsPrice" />
<asp:HyperLinkField DataNavigateUrlFields="GoodsID" DataNavigateUrlFormatString="Default2.aspx?id={0}"
HeaderText="查看详细信息" Text="详细信息" />
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="选择项" ShowSelectButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged"
Text="全选" AutoPostBack="True" /></td>
<td style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询