网页里用datagrid1将1个数据表分页显示出来,同时网页里还放有一个dropdownlist(ID=dropCat),供用户选择数据表中“类型”字段的值,再用查询按钮查询(ID=btnSearch)。网页初始加载时显示数据表里所有内容,需要显示某一类型的内容,在dropCat里选好类型后点击查询按钮,datagrid1就将该类型的信息显示出来。
现在问题是这样,第一次进到这个页面,也就是在第一页时,选好类型查询没有一点问题,但是如果我先翻页,比如到第二页,再选类型查询就会出错,提示“Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount”。我在网页里用了label来显示CurrentPageIndex和PageCount的。
所以我就不明白了,难道翻了页后的dataview(datagrid1的数据源是dataview)就变了?
现在问题是这样,第一次进到这个页面,也就是在第一页时,选好类型查询没有一点问题,但是如果我先翻页,比如到第二页,再选类型查询就会出错,提示“Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount”。我在网页里用了label来显示CurrentPageIndex和PageCount的。
所以我就不明白了,难道翻了页后的dataview(datagrid1的数据源是dataview)就变了?
解决方案 »
- GridView多条件查询 分页问题!!!!?
- FileUpload 问题
- ASP.NET中OnClientClick属性可以用于页面跳转吗?怎么跳转?
- web.config文件中能不能支持多个appsettings外部文件啊
- 格式化浮点数问题,大家来帮我看看.
- asp.net2.0 还需要安装iis吗?
- 怎样配置用owc显示图表
- 请教正则表达式代码,将<a href="xxx">yyy</a>中的yyy部分取出来
- 我准备学ASP.NET,装了IIS,VS.NET,为什么提示服务器应用程序不可用?
- 我想用ASP.NET开发网页及别的project。需要准备哪些环境?装一些什么软件才能支持运行?
- listbox怎么取得所有选择了的项的值
- 如何利用ADO.net将两个不同的数据库合并,分快用完了,一点小意思,谢谢大家啦!
dim dv as dataview
dim dt as datatable
sub page_load(sender as object, e as eventargs)
dim provider,SQL,ConnStr as string
provider="microsoft.jet.oledb.4.0;"
ConnStr = "Provider="+Provider+"Data Source="+server.mappath("/db/Archives.mdb") +";Persist Security Info=False;User ID=;Jet OLEDB:Database password=123"
SQL="select * from Archives"
dim cmd as oledbdataadapter
cmd=new oledbdataadapter(SQL,ConnStr)
dim ds as dataset=new dataset()
cmd.fill(ds,"Archives")
dt=ds.tables("Archives")
dv=new dataview(dt)
DataGrid1.DataSource = dv
DataGrid1.DataBind
else
response.redirect("warning4.aspx")
end if
end sub
Sub DataGrid1_Page(sender As Object, e As DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid
End Sub
sub btnSearch_Click(sender as object,e as eventargs)
dim provider,SQL,ConnStr as string
provider="microsoft.jet.oledb.4.0;"
ConnStr = "Provider="+Provider+"Data Source="+server.mappath("/db/Archives.mdb") +";Persist Security Info=False;User ID=;Jet OLEDB:Database password=123"
if dropCat.SelectedIndex<>0 then
SQL="select * from Archives where Archives_Cat='"& dropCat.SelectedItem.text &""
else
SQL="select * from Archives"
end if
dim cmd as oledbdataadapter
cmd=new oledbdataadapter(SQL,ConnStr)
dim ds as dataset=new dataset()
cmd.fill(ds,"Archives")
dt=ds.tables("Archives")
dv=new dataview(dt)
DataGrid1.DataSource = dv
DataGrid1.DataBind
end sub
*****************************************************
希望各位能有耐心看我的帖子,鞠躬!
if( !this.IsPostBack)
{
dg.DataBind();//在这里进行绑定
}
所以,你最好在分类查询前将DataGrid.CurrentPageIndex = 0
#region Page_Seperate
public void BindData(string Sql,string Sql1,DataGrid DataGrid1,Wuqi.Webdiyer.AspNetPager AspNetPager1,string DsName)
{
try
{
ConnDB();
MyConn.Open();
SqlCommand MyComm = new SqlCommand(Sql, MyConn);
AspNetPager1.RecordCount = (int)MyComm.ExecuteScalar();
SqlCommand MyComm1 = new SqlCommand(Sql1, MyConn);
SqlDataAdapter Adapter = new SqlDataAdapter(MyComm1);
DataSet Ds = new DataSet();
Adapter.Fill(Ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize,DsName);
DataGrid1.DataSource = Ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
catch(Exception ex)
{
throw new Exception(ex.Source +ex.Message +ex.StackTrace);
}
}
#endregion
所以在你选择类型之后,首先把DataGrid.CurrentPageIndex = 0。
但是这样又会出现另一个问题(如果你的DataGrid.CurrentPageIndex = 0在绑定代码之中),
你点翻页的时候永远只会在第一页,因为CurrentPageIndex,始终等于0,
所以选择类型之后绑定和翻页绑定因为有不同的方法,而代码就是一个存在DataGrid.CurrentPageIndex = 0(选择类型绑定),另一个则不存在(翻页绑定)