在页面加载的时候,执行了一次GirdView的绑定,显示的是ID=2的,当我点按钮的时候,会把这个表中所有信息显示出来,每页显示5条,当我点第二页的时候,第二页的内容不出来,还是显示ID=2的那条。这是怎么回事?
代码如下:
SqlConnection con;
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("server=.;uid=sa;database=pubs");
if (!IsPostBack)
{
GridViewDataBind();
}
}
public void GridViewDataBind()
{
string SQLstr = "select * from jobs where job_id =2";
da = new SqlDataAdapter(SQLstr,con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewDataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from jobs",con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
代码如下:
SqlConnection con;
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("server=.;uid=sa;database=pubs");
if (!IsPostBack)
{
GridViewDataBind();
}
}
public void GridViewDataBind()
{
string SQLstr = "select * from jobs where job_id =2";
da = new SqlDataAdapter(SQLstr,con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewDataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from jobs",con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
解决方案 »
- Excel 导人出现问题
- 大家来看看,做个像中关村模拟装机平台这样的网站要多少钱啊?
- gridview的列到底应当怎样对齐啊
- -------div排版的问题
- 请教一个在.net中用OWC11作图的问题
- 在ASP中的常见用法,可我在ASP.NET中不会用,高手帮我,新手顶我啊,谢谢
- 在.NET(VB)中实现弹出式菜单
- 在datagrid中<ItemTemplate></ItemTemplate>中有一个textbox,我的原意是只能输入整数,但是当输入了非整数后,提交后,程序报错,如何能
- 关于防止WebAPi被第三方网站盗用
- 怎样用insert批量插入记录
- 关于在IIS中配置网站...
- datalist嵌套使用,现在是要鼠标滑到二级目录时鼠标右侧弹出出来一个小框,里面显示三级目录,用div+js来做!帮忙!
你可以用断点调试一下看看,过程是怎么执行的!!
看看在点击button的时候执行的是什么步骤!!
按钮的时候它应该不会执行PageIndexchanging事件!!
==========>
分页事件里写的是获取ID=2的数据记录集,当然显示ID=2的那条。
第二页的内容不出来,你确定ID=2的记录数有>5条吗?
=========================================================
我就是想在分页绑定的时候想想办法,怎么样能绑定两次,页面加载的时候绑定的是ID=2的数据,当点第二页的时候,显示的是button绑定的数据
{
GridView1.PageIndex = e.NewPageIndex;
GridViewDataBind();//问题出在这里
}
当你点击第二页时,执行GridViewDataBind方法,而GridViewDataBind方法是取ID=2的数据。
{
da = new SqlDataAdapter("select * from jobs",con);
da.Fill(ds);GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
点击BUTTON后你的DG的数据源已经变了,再点下一页,当然不是你想要的JOBS列表的下一页了
{
GridView1.PageIndex = e.NewPageIndex;
//GridViewDataBind();
Button1_Click(this.Button1, EventArgs.Empty) }
static int m=0;
SqlConnection con;
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("server=.;uid=sa;database=pubs");
if (!IsPostBack)
{
GridViewDataBind();
}
}
public void GridViewDataBind1()
{
m=0;
string SQLstr = "select * from jobs where job_id =2";
da = new SqlDataAdapter(SQLstr,con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
public void GridViewDataBind2()
{
m=1;
string SQLstr = "select * from jobs";
da = new SqlDataAdapter(SQLstr,con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if(m==0)
GridViewDataBind1();
if(m==1)
GridViewDataBind2();
}
protected void Button1_Click(object sender, EventArgs e)
{
m=1;
da = new SqlDataAdapter("select * from jobs",con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
http://bbs.msproject.cn/Default.aspx?g=posts&t=434
protected void Button1_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter( "select * from jobs ",con);
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
}
SqlDataAdapter被两次赋值了,第二次是在GridView1.DataBind(); 中。
请注意。