问题描述
请问大家,DataList与Repeater如何实现分页?
解决方案
解决方案二:
用pagedatasource,如果有需要,我把源代码给你其实你也可以看一下这个http://www.lokcore.com/avrilxu/article.asp?id=5repeater的详解
解决方案三:
谢谢
解决方案四:
可以试用pagedatasource这个类,提供了分页的功能,不过数据量太大的话效率不是很高。我的一个例子楼主可以看一下:publicpartialclassRepeterExcerise:System.Web.UI.Page{stringConStr=ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;SqlConnectionCon;protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){//设置默认的排序顺序ViewState["CompanyName"]="SupplierID";ViewState["Direction"]="ASC";ViewState["LineNo"]=0;//初始化的时候默认显示第一页lblCount.Text="1";BindRepeter();}}//判断命令protectedvoidRepeater1_ItemCommand(objectsource,RepeaterCommandEventArgse){StringIndex=((Label)Repeater1.Items[e.Item.ItemIndex].FindControl("lblID")).Text;Session["ID"]=Index;//编辑模式if(e.CommandName=="Edit"){stringCompanyName=((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblCompanyName"))).Text;stringName=((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblName"))).Text;stringAddress=((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblAddress"))).Text;stringCity=((Label)(Repeater1.Items[e.Item.ItemIndex].FindControl("lblCity"))).Text;txtCompanyName.Text=CompanyName;txtContactName.Text=Name;txtAddress.Text=Address;txtCity.Text=City;if(Panel1.Visible==false){ShowMessage("进入编辑模式");}SetDisplayTrue();btnInsert.Visible=false;btnUpdate.Visible=true;Button1.Visible=true;}//删除elseif(e.CommandName=="Delete"){if(Panel1.Visible==true){ShowMessage("编辑模式下禁止删除");}else{SetDisplayFalse();stringDeleteStr="DELETEFROMSuppliersWHERESupplierID='"+Index+"'";Con=newSqlConnection(ConStr);SqlCommandDeleteCmd=newSqlCommand(DeleteStr,Con);try{Con.Open();DeleteCmd.ExecuteNonQuery();ShowMessage("删除成功");BindRepeter();}catch(Exceptionex){ShowMessage("删除有错误,请检查");}finally{Con.Dispose();}}}//新增elseif(e.CommandName=="New"){if(Panel1.Visible==true){ShowMessage("编辑模式下禁止添加新行");}else{SetDisplayTrue();ClearWord();btnInsert.Visible=true;btnUpdate.Visible=false;Button1.Visible=true;}}}//绑定RepeterprivatevoidBindRepeter(){stringQueryCon="SELECTSupplierID,CompanyName,ContactName,Address,CityFROMSuppliers";Con=newSqlConnection(ConStr);SqlDataAdapterNorthWindDa=newSqlDataAdapter(QueryCon,ConStr);DataSetDs=newDataSet();NorthWindDa.Fill(Ds,"Suppliers");//Repeater1.DataKeyNames=newstring[]{"SupplierID"};DataViewDv=Ds.Tables["Suppliers"].DefaultView;//排序表达式stringSortExpress=(string)ViewState["CompanyName"]+""+(string)ViewState["Direction"];Dv.Sort=SortExpress;//绑定数据源//Repeater1.DataSource=Ds.Tables["Suppliers"];//利用PagedDataSource进行分页PagedDataSourcePageDs=newPagedDataSource();PageDs.DataSource=Dv;PageDs.AllowPaging=true;PageDs.PageSize=10;intCurrentPage=Convert.ToInt32(lblCount.Text);//当前页PageDs.CurrentPageIndex=CurrentPage-1;btnPage.Enabled=true;Button2.Enabled=true;if(CurrentPage==1){Button2.Enabled=false;}if(CurrentPage==PageDs.PageCount){btnPage.Enabled=false;}//PageCount=PageDs.PageCount;lblPage.Text="共"+PageDs.PageCount+"页,当前在第";//将PageDs作为数据源进行绑定Repeater1.DataSource=PageDs;Repeater1.DataBind();}//显示编辑区域privatevoidSetDisplayTrue(){lblCompanyName.Visible=true;lblName.Visible=true;lblAddress.Visible=true;lblCity.Visible=true;txtCompanyName.Visible=true;txtContactName.Visible=true;txtAddress.Visible=true;txtCity.Visible=true;Panel1.Visible=true;}//隐藏编辑区域privatevoidSetDisplayFalse(){lblCompanyName.Visible=false;lblName.Visible=false;lblAddress.Visible=false;lblCity.Visible=false;txtCompanyName.Visible=false;txtContactName.Visible=false;txtAddress.Visible=false;txtCity.Visible=false;Panel1.Visible=false;}}
解决方案五:
privatevoidClearWord(){txtCompanyName.Text="";txtContactName.Text="";txtAddress.Text="";txtCity.Text="";}//封装javascriptprivatevoidShowMessage(stringMessage){LiteralTxtMsg=newLiteral();TxtMsg.Text="<script>alert('"+Message+"')</script>";Page.Controls.Add(TxtMsg);}//更新数据protectedvoidbtnUpdate_Click(objectsender,EventArgse){/*在有数据输入的时候应该检查输入数据的合法性,*过滤掉特殊的脚本,特殊的字符串,提高安全性*/stringCompanyName=Server.HtmlEncode(txtCompanyName.Text.ToString());stringContactName=Server.HtmlEncode(txtContactName.Text.ToString());stringAddress=Server.HtmlEncode(txtAddress.Text.ToString());stringCity=Server.HtmlEncode(txtCity.Text.ToString());stringUpdateStr="UPDATESuppliersSETCompanyName='"+CompanyName+"',ContactName='"+ContactName+"',"+"Address='"+Address+"',City='"+City+"'WHERESupplierID='"+Session["ID"]+"'";Con=newSqlConnection(ConStr);SqlCommandUpdateCmd=newSqlCommand(UpdateStr,Con);try{Con.Open();UpdateCmd.ExecuteNonQuery();ShowMessage("更新成功");SetDisplayFalse();BindRepeter();}catch(Exceptionex){ShowMessage("更新出错,请重新输入");}finally{Con.Dispose();}}protectedvoidButton1_Click(objectsender,EventArgse){SetDisplayFalse();}//插入一行新数据protectedvoidbtnInsert_Click(objectsender,EventArgse){stringCompanyName=Server.HtmlEncode(txtCompanyName.Text.ToString());stringContactName=Server.HtmlEncode(txtContactName.Text.ToString());stringAddress=Server.HtmlEncode(txtAddress.Text.ToString());stringCity=Server.HtmlEncode(txtCity.Text.ToString());stringInsertStr="INSERTINTOSuppliers(CompanyName,ContactName,Address,City)VALUES('"+CompanyName+"','"+ContactName+"','"+Address+"','"+City+"')";Con=newSqlConnection(ConStr);SqlCommandInsertCmd=newSqlCommand(InsertStr,Con);try{Con.Open();InsertCmd.ExecuteNonQuery();ShowMessage("插入成功");SetDisplayFalse();BindRepeter();}catch(Exceptionex){ShowMessage("插入失败,请重新检查");}finally{Con.Dispose();}}//给删除按钮添加客户端的Confirm对话框protectedvoidRepeater1_ItemDataBound(objectsender,RepeaterItemEventArgse){if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item){((Button)Repeater1.Controls[Repeater1.Controls.Count-1].FindControl("Delete")).Attributes["onclick"]="if(!confirm('你真的要删除这条记录么?'))returnfalse;";}//switch(e.Item.ItemType)//{//caseListItemType.AlternatingItem://((Label)Repeater1.Controls[Repeater1.Controls.Count-4].FindControl("lblID")).BackColor=Color.AliceBlue;//break;//caseListItemType.Item://((Label)Repeater1.Controls[Repeater1.Controls.Count-4].FindControl("lblID")).BackColor=Color.AliceBlue;//break;//}}/*privatevoidPageView(intIndex){if(Session["Dv"]!=null){PagedDataSourcePageSource=newPagedDataSource();PageSource.AllowPaging=true;PageSource.PageSize=10;PageSource.DataSource=((DataView)Session["Dv"]);PageSource.CurrentPageIndex=Index;//lblCount.Text="共"+PageSource.PageCount+"页,当前第"+(PageSource.CurrentPageIndex+1)+"页";Repeater1.DataSource=PageSource;Repeater1.DataBind();}}*///下一页protectedvoidbtnPage_Click(objectsender,EventArgse){//更改pageds的当前页数lblCount.Text=Convert.ToString(Convert.ToInt32(lblCount.Text)+1);BindRepeter();}//上一页protectedvoidButton2_Click(objectsender,EventArgse){//更改当前页lblCount.Text=Convert.ToString(Convert.ToInt32(lblCount.Text)-1);BindRepeter();}//排序protectedvoidbtnSort_Click(objectsender,EventArgse){//判断排序表达式的顺序if(ViewState["Direction"].ToString()=="DESC"){ViewState["Direction"]="ASC";}else{ViewState["Direction"]="DESC";}/*设置完排序的顺序以后,将排序后的dataview*绑定到repeter上。这个方法,目前有个缺陷,*每次排序的时候都是将整个数据源进行排序,*并不是对当前的分页进行排序。*/BindRepeter();}
解决方案六:
mark
解决方案七:
Repeater自定义分页首页上一页下一页尾页<scriptrunat="server">errorsers=newerrors();protectedvoidPage_Load(objectsender,EventArgse){DataSetds=null;if(Cache["Repeater"]!=null){ds=(DataSet)Cache["Repeater"];}else{Cache.Insert("Repeater",(DataSet)CreateCache());ds=(DataSet)Cache["Repeater"];}PageBind(ds);}privateDataSetCreateCache(){errorser=newerrors();DataSetds=er.GetErrors();returnds;}protectedvoidrepErrors_ItemDataBound(objectsender,RepeaterItemEventArgse){if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem){HtmlInputHiddenhi=(HtmlInputHidden)e.Item.FindControl("Hidden1");Repeaterrep=(Repeater)e.Item.FindControl("repErrorinfo");rep.DataSource=ers.GetErrorInfo(Int32.Parse(hi.Value));rep.DataBind();}}privatevoidPageBind(DataSetds){PagedDataSourceobjps=newPagedDataSource();objps.DataSource=ds.Tables[0].DefaultView;objps.AllowPaging=true;objps.PageSize=2;intCurPage;if(Request.QueryString["Page"]!=null){CurPage=Int32.Parse(Request.QueryString["Page"].ToString());}else{CurPage=1;}objps.CurrentPageIndex=CurPage-1;Label1.Text=objps.PageCount.ToString();Label2.Text=CurPage.ToString();LinkButton1.PostBackUrl=Request.CurrentExecutionFilePath+"?Page=1";if(!objps.IsFirstPage)LinkButton2.PostBackUrl=Request.CurrentExecutionFilePath+"?Page="+(CurPage-1);if(!objps.IsLastPage)LinkButton3.PostBackUrl=Request.CurrentExecutionFilePath+"?Page="+(CurPage+1);LinkButton4.PostBackUrl=Request.CurrentExecutionFilePath+"?Page="+objps.PageCount.ToString();repErrors.DataSource=objps;repErrors.DataBind();}protectedvoidButton1_Click(objectsender,EventArgse){DataSetds=null;if(Cache["Repeater"]!=null){ds=(DataSet)Cache["Repeater"];}PageBind(ds);}</script><divstyle="text-align:center"><divstyle="text-align:center"><divstyle="text-align:center"><table><tr><tdalign="left"><asp:RepeaterID="repErrors"runat=serverOnItemDataBound="repErrors_ItemDataBound"><HeaderTemplate><tableborder=1style="border-right:gray0pxsolid;border-top:gray0pxsolid;border-left:gray0pxsolid;border-bottom:gray0pxsolid"width=800px></HeaderTemplate><ItemTemplate><tr><td><inputid="Hidden1"type="hidden"value='<%#Eval("exNodeID")%>'runat=server/></td></tr><tr><tdalign=left><imgsrc="images/xinxi.gif"/> <%#Eval("exDate","{0:yyyy年M月d日}")%></td></tr><tr><tdalign=left><asp:RepeaterID="repErrorinfo"runat=server><HeaderTemplate><table></HeaderTemplate><ItemTemplate><tr><tdalign=left><fontstyle="color:blue"><%#Eval("exMessage")%></font></td></tr><tr><tdalign=right><%#Eval("exDateTime")%> <asp:LinkButtonID="LinkButton1"runat="server"PostBackUrl='<%#"~/Default2.aspx?exid="+Eval("exID")%>'><fontstyle="color:Gray">详细信息</font></asp:LinkButton></td></tr></ItemTemplate><FooterTemplate></table></FooterTemplate></asp:Repeater></td></tr></ItemTemplate><FooterTemplate></table></FooterTemplate></asp:Repeater></td></tr><tr><tdalign="left">共<asp:LabelID="Label1"runat="server"></asp:Label>页,当前第<asp:LabelID="Label2"runat="server"></asp:Label>页,<asp:LinkButtonID="LinkButton1"runat="server">首页</asp:LinkButton><asp:LinkButtonID="LinkButton2"runat="server">上一页</asp:LinkButton><asp:LinkButtonID="LinkButton3"runat="server">下一页</asp:LinkButton><asp:LinkButtonID="LinkButton4"runat="server">尾页</asp:LinkButton><asp:TextBoxID="TextBox1"runat="server"Width="67px"></asp:TextBox><asp:ButtonID="Button1"runat="server"Text="[GO]"Width="40px"OnClick="Button1_Click"/></td></tr></table></div></div></div>
解决方案八:
mark
解决方案九:
pagedatasource
解决方案十:
呵呵,实现了,谢谢哦.