类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
说明:这个页面做了一个搜索功能.页面默认载入初始数据并绑定GridData控件,其分页功能完全正常.但是按搜索功能后,有时会出现如下面的错误提示:
1.无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。
2.另外还有一种错误情况是,如果按部门搜索到多于一页记录,比如有三页,那么第一页显示正常的,当按下一页时则变成页面默认载入时的页面了.而不是刚刚搜索到的数据.请问这是怎么回事.
另外,再向各位高手请教,我觉得我的程序写的很繁烦,如何改进?
namespace ryb
{
.................
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.butFirst.Click += new System.EventHandler(this.PagerButtonClick);
this.butPrev.Click += new System.EventHandler(this.PagerButtonClick);
this.butNext.Click += new System.EventHandler(this.PagerButtonClick);
this.butLast.Click += new System.EventHandler(this.PagerButtonClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
butFirst.Text="最首页";
butPrev.Text="前一页";
butNext.Text="下一页";
butLast.Text="最末页";
//DataGrid控件初始化
if(!IsPostBack)
{
getdepname();
BindGrid();
Showstats();
Label1.Text=DataGrid1.Items.Count.ToString();
}
}
//初始化部门控件值
private void getdepname()
{
Drp_dep.Items.Clear();
SqlConnection conn=new SqlConnection(path);
conn.Open();
SqlCommand cmd=new SqlCommand("sp_seldepname",conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader myDataReader=cmd.ExecuteReader();
Drp_dep.Items.Add("所有部门");
while(myDataReader.Read()==true)
{
Drp_dep.Items.Add(myDataReader.GetString(1).ToString());
}
conn.Close();
}
private void BindGrid()
{
SqlConnection con=new SqlConnection(path);
SqlCommand cmd=new SqlCommand("sp_showhumanlist",con);
con.Open();
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure; // use stored proc for perf
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=DataGrid1.Items.Count.ToString();
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
Label1.Text=DataGrid1.Items.Count.ToString();
}
private void Showstats()
{
lblCurrentIndex.Text="第"+(DataGrid1.CurrentPageIndex+1).ToString()+"页";
lblPagecount.Text="总共"+DataGrid1.PageCount.ToString()+"页";
}
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case "next":
if (DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1))
{
DataGrid1.CurrentPageIndex ++;
DataGrid1.DataBind();
}
break;
case "prev":
if (DataGrid1.CurrentPageIndex > 0)
{
DataGrid1.CurrentPageIndex --;
DataGrid1.DataBind();
}
break;
case "last":
DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1);
DataGrid1.DataBind();
break;
default:
DataGrid1.CurrentPageIndex = System.Convert.ToInt32(arg);
DataGrid1.DataBind();
break;
}
BindGrid();
Showstats();
}
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection(path);
conn.Open();
//假设所选部门为所有部门
if(Drp_dep.SelectedItem.Value.Trim().ToString()=="所有部门")
{
//假设所选部门为所有部门,姓名为空的条件下
if(human_name.Text.Trim().ToString()=="")
{
string strsql="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human";
SqlCommand cmd=new SqlCommand(strsql,conn);
cmd.CommandType=CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter();
DataSet ds=new DataSet();
dap.SelectCommand = cmd;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
//假设所选部门为所有部门,但姓名不为空的条件下
else
{
string strname=human_name.Text.Trim().ToString();
string strsql_sybm_havename="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where name=" + "" +strname+"";
SqlCommand cmd1=new SqlCommand(strsql_sybm_havename,conn);
cmd1.CommandType=CommandType.Text;
SqlDataAdapter adp=new SqlDataAdapter();
DataSet ds=new DataSet();
adp.SelectCommand=cmd1;
adp.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
//省略判断语句,查到数据库中有记录,并显示
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
}
}
//所选部门不为所有部门的处理
else
{
//所选部门不为所有部门,所有人员信息
if(human_name.Text.Trim().ToString()=="")
{
string depname=Drp_dep.SelectedItem.Value.Trim().ToString();
string strsql_depname="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where depment=" + ""+depname+"";
SqlCommand cmd2=new SqlCommand(strsql_depname,conn);
cmd2.CommandType=CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter();
DataSet ds=new DataSet();
dap.SelectCommand = cmd2;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
//所选部门不为所有部门,显示按姓名搜索结果
else
{
string depname=Drp_dep.SelectedItem.Value.Trim().ToString();
string strname=human_name.Text.Trim().ToString();
string strsql_depname_name="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where depment=" + ""+depname+""+" "+"and"+" "+"name="+""+strname+"";
SqlCommand cmd3=new SqlCommand(strsql_depname_name,conn);
cmd3.CommandType=CommandType.Text;
SqlDataAdapter dap3 = new SqlDataAdapter();
DataSet ds=new DataSet();
dap3.SelectCommand = cmd3;
dap3.Fill(ds);
... 省略判断......
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}}}}}}
网友回答: