想完成一个
"点击button后查找出数据表U中后缀如@163.com的电子邮件.并且以分页的形式显示在DATAGRID上"
的功能
程序如下.TextBox1.Text是输入163.com或者sina.com.cnprivate void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}private void Button1_Click(object sender, System.EventArgs e)
{
BindGrid();
}private void BindGrid()
{
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
this.sqlConnection1.Open();
this.sqlCommand1.CommandText="select * from U where EMAIL like '%@"+TextBox1.Text+"'";
da.SelectCommand=this.sqlCommand1;
da.Fill(ds,"U");
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
this.sqlConnection1.Close();}private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
DataGrid1.DataBind();
}
}现在的情况是分页功能顺利实现.
但是出现郁闷的问题是:
我的表里面有10条@163.com和1条@sina.com.cn为后缀的邮件记录.
我查询@163.com.查出来10条分页记录后后对DATAGRID进行翻页查看操作!!!!
然后我再查询@sina.com.cn时,出现了以下错误!!!:
无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount请问这是怎么回事,程序应该是对的呀,为什么会有冲突...
"点击button后查找出数据表U中后缀如@163.com的电子邮件.并且以分页的形式显示在DATAGRID上"
的功能
程序如下.TextBox1.Text是输入163.com或者sina.com.cnprivate void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}private void Button1_Click(object sender, System.EventArgs e)
{
BindGrid();
}private void BindGrid()
{
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
this.sqlConnection1.Open();
this.sqlCommand1.CommandText="select * from U where EMAIL like '%@"+TextBox1.Text+"'";
da.SelectCommand=this.sqlCommand1;
da.Fill(ds,"U");
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
this.sqlConnection1.Close();}private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
DataGrid1.DataBind();
}
}现在的情况是分页功能顺利实现.
但是出现郁闷的问题是:
我的表里面有10条@163.com和1条@sina.com.cn为后缀的邮件记录.
我查询@163.com.查出来10条分页记录后后对DATAGRID进行翻页查看操作!!!!
然后我再查询@sina.com.cn时,出现了以下错误!!!:
无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount请问这是怎么回事,程序应该是对的呀,为什么会有冲突...
解决方案 »
- 求一个正则表达式.字符里有括号咋匹配?比如:l like the(123)
- 查询sqlserver表
- asp.net 请问如何控制label显示的字数长度
- 无客户端怎么连接oracle服务器
- 控件与样式表的问题,急知,在线等
- 控件的视图状态的问题,谢谢
- 如何判断Calendar日期是上旬,中旬还是下旬?
- 本人是ASP.NET的初学者,没有实践经验,现在想到公司实习(不求工资,只要在上海就好了)
- 我不会的一个问题,请问如何控制一个客户端BUTTON变为可用与不可用,最好是可见与不可见.谢谢各位大侠.本人是小菜
- 如何在一个类里实现页面跳转?
- 泡妞三十六计2.0版(二)
- DataGrid的一个问题,大家帮我看一下!
我查询@163.com.查出来10条分页记录后
如果没有对DATAGRID进行翻页操作!
那么再查询@sina.com.cn时候.就不会出错!
在BindGrid中用try-catch语句把代码括起来
在catch块中,写一条语句:DataGrid1.CurrentPageIndex =0
然后再绑定:BindGrid(如果不写这一句的话,接着的一次不回显示)如下所示:
private void BindGrid()
{
try{
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
this.sqlConnection1.Open();
this.sqlCommand1.CommandText="select * from U where EMAIL like '%@"+TextBox1.Text+"'";
da.SelectCommand=this.sqlCommand1;
da.Fill(ds,"U");
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
this.sqlConnection1.Close();
}
catch{
'把当前页设为0,即第一页
DataGrid1.CurrentPageIndex =0
'再调用它自己
BindGrid()
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
BindGrid();
DataGrid1.CurrentPageIndex =0; }
刚测试下好象就OK了...还没具体测试...是不是这样改就行了呢?
liangfengxx(莫拿你的成绩证明你的能力)兄你的程序有错误哦
DataGrid1.DataBind();第二句好像是多余的
{
DataGrid1.CurrentPageIndex =0;
BindGrid();
}
在论坛里见过不少网友问过这样的问题:当DataGrid翻到最后一页后,如果当前页的数据只有一条,那么删除这条记录就会出现“异常详细信息: System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。”这样的错误,出现这个错误的原因是当最后一页上的这条记录被删除并重新绑定数据之后,DataGrid的总页数减少了一页,而它的CurrentPageIndex属性值却仍然是删除记录前最后一页的页索引值,这个值超出了它的总页数。一些网友解决这个问题的方法是:在删除记录之后立即将DataGrid的CurrentPageIndex设为0,即跳回第一页,这个方法虽然可以避免再发生这样的错误,然而却很不灵活,因为如果用户要删除最后一页上的两条记录,那么在删除了其中一条后,不得不重新点击分页按钮进入最后一页来删除第二条,而且很多时候这种方法会给用户带来困扰,因为很容易把跳转到的第一页的数据当成是正执行删除命令的最后一页的数据,其实这个问题有更好的解决办法,如果我们用的是DataGrid的内建的删除功能,即一次只能删除一条记录的话,那么在它的DeleteCommand事件处理程序中,可以通过下面的方法,来实现当最后一页没有数据时,自动跳回前一页,否则只需要重新绑定数据即可:void DataGrid1_Delete(object src,DataGridCommandEventArgs e){
//执行删除命令,从数据库或其它保存数据的地方删除DataGrid中对应的记录(略)
//如果当前页上只有一条数据并且DataGrid的当前页索引大于0,那么就让DataGrid返回上一页
if(DataGrid1.Items.Count==1&&DataGrid1.CurrentPageIndex>0){
DataGrid1.CurrentPageIndex--;
}
//重新绑定数据
BindData();
}如果用了模板列中的CheckBox,实现一次删除多条选中的记录的话,那么可以在删除按钮的Click事件处理程序中这样来实现上面的功能:
void deleteButton_Click(object src,EventArgs e){
int deletedRows=0; //记录删除掉的行数
foreach(DataGridItem item in DataGrid1.Items){
if(item.ItemType==ListItemType.Item||item.ItemType==ListItemType.AlternatingItem){
CheckBox ckbox=item.FindControl("delckbox") as CheckBox;
if(ckbox!=null&&ckbox.Checked){
deletedRows++;
//从数据库或其它保存数据的地方删除DataGrid中对应的记录(略)
}
}
}
//如果当前页的记录总数等于已经被删除掉的记录数并且DataGrid当前页索引大于0,那么让DataGrid返回上一页
if(deletedRows==DataGrid1.Items.Count&&DataGrid.CurrentPageIndex>0){
DataGrid1.CurrentPageIndex--;
}
//重新绑定数据
BindData();
}