• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

一直没解决的问题,难倒了好几个高手了!请高高高手帮忙

rybgroup 2004-11-01 12:41:07
问题是我按查询时,如果查询结果有多个页,按下一页或者页码时,则数据是页面默认载入的数据,而不是查询的结果数据.请问该怎么解决?

namespace ryb
{
/// <summary>
/// list_huamn 的摘要说明。
/// </summary>
public class list_huamn : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton butFirst;
protected System.Web.UI.WebControls.LinkButton butPrev;
protected System.Web.UI.WebControls.LinkButton butNext;
protected System.Web.UI.WebControls.LinkButton butLast;
protected System.Web.UI.WebControls.Label lblPagecount;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList Drp_dep;
protected System.Web.UI.WebControls.TextBox human_name;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
static string path=ConfigurationSettings.AppSettings["ConnectionString"];
protected System.Web.UI.WebControls.Label Label2;
string seachtext="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";
DataSet ds = new DataSet();
.....
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)
{
//验证用户权限
checkuser_quanxian();
// 在此处放置用户代码以初始化页面
butFirst.Text="最首页";
butPrev.Text="前一页";
butNext.Text="下一页";
butLast.Text="最末页";


//DataGrid控件初始化
if(!IsPostBack)
{
BindGrid();
getdepname();
Showstats();
}

}
//初始化部门控件值
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 checkuser_quanxian()
{

SqlConnection conn=new SqlConnection(path);
conn.Open();
SqlCommand cmd=new SqlCommand("sp_selquanxian",conn);
SqlDataAdapter adp=new SqlDataAdapter();
adp.SelectCommand=cmd;

//建立存储过程连接
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter struserid=adp.SelectCommand.Parameters.Add("@user_id",SqlDbType.Int);
//赋值ID参数
struserid.Value=Session["loguserid"];
DataSet ds1=new DataSet();
adp.Fill(ds1,"quanxian");
DataRow dr;
dr=ds1.Tables["quanxian"].Rows[0];
if(int.Parse(dr.Table.Rows.Count.ToString())>0)
{
if(dr["hr_ygda_seach"].ToString()==true.ToString())
{

}
else
{
Response.Write("<Script>alert('对不起,您没有权限使用该模块功能,请与系统管理员联系');location='index_right.aspx';</Script>");

}
}
else
{
Response.Write("<Script>alert('对不起,您没有权限使用该模块功能,请与系统管理员联系');location='index_right.aspx';</Script>");
}
conn.Close();
}

private void BindGrid()
{

SqlConnection conn=new SqlConnection(path);
conn.Open();
SqlCommand cmd=new SqlCommand(seachtext,conn);
cmd.CommandType = CommandType.Text; // 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=ds.Tables[0].Rows.Count.ToString();
Label2.Text=DataGrid1.Items.Count.ToString();
}

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
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)
{
DataGrid1.CurrentPageIndex=0;
//处理部门为所有部门的查询.
if(Drp_dep.SelectedItem.Value.Trim().ToString()=="所有部门")
{
//再处理是否按姓名查询
//姓名为空的处理
if(human_name.Text.Trim().ToString()=="")
{
seachtext="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";
BindGrid();
}
//姓名不为空的处理
else
{
string strname=human_name.Text.Trim().ToString();
seachtext="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+"'";
BindGrid();
}
}
//处理部门为个别部门的查询
else
{
//姓名为空的处理
if(human_name.Text.Trim().ToString()=="")
{
seachtext="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="+"'"+Drp_dep.SelectedItem.Text.Trim().ToString()+"'";
BindGrid();
}
//姓名不为空的处理
else
{
seachtext="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="+"'"+Drp_dep.SelectedItem.Text.Trim().ToString()+"'"+" "+"and name="+"'"+human_name.Text.Trim().ToString()+"'";
BindGrid();
}
}
}
}
}
...全文
68 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
我是老姚 2004-11-01
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.Page.IsPostBack){
你的代码
}
加上这个应该可以!
回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-11-01 12:41
社区公告

让您成为最强悍的C#开发者