Datatable paging,Repeater with Paging
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | /// <summary> /// 塗聚文 /// 20140225 /// </summary> public partial class DatatablePage : System.Web.UI.Page { /// <summary> /// 测试 分页数据 /// </summary> DataTable pageDt; /// <summary> /// /// </summary> public void geovinduDt() { pageDt = new DataTable(); pageDt.Columns.Add( "id" , typeof ( int )); pageDt.Columns.Add( "ddno" , typeof ( string )); pageDt.Columns.Add( "dd" , typeof ( string )); //dtyhdd.Rows.Add("1", "n1"); //dtyhdd.Rows.Add("2", "n2"); for ( int i = 1; i <= 31; i++) { //保存到内存表中 DataRow tr = this .pageDt.NewRow(); tr[ "id" ] = i; tr[ "ddno" ] = i.ToString(); tr[ "dd" ] = i.ToString() + "内容" ; pageDt.Rows.Add(tr); } } /// <summary> /// 隐患地点共分多少页 /// </summary> int verypage = 0; /// <summary> /// 隐患当前页 /// </summary> int currentPage = 0; /// <summary> /// 每页显示多少数据 /// </summary> int PageSize = 1; /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { if (! string .IsNullOrEmpty(Request.QueryString[ "page" ])) { currentPage = int .Parse(Request.QueryString[ "page" ]); } currentPage++; this .TextBox1.Text = currentPage.ToString(); //【初始化数据】 geovinduDt(); //【执行分页显示】 //每次显示10条数据 需要多少次 verypage = pageDt.Rows.Count / 1; //是否有余数 int numys = pageDt.Rows.Count % 1; if (numys > 0) { currentPage++; } //comyhdd.Items.Clear(); if (currentPage >= pageDt.Rows.Count) { Button1.Enabled = false ; return ; } DataRow[] mMatches = pageDt.Select( "(id=" + currentPage + ")" ); //DataRow matches = new DataRow(); string strName = mMatches[0][ "dd" ].ToString(); DataSet pDS = new System.Data.DataSet(); System.Data.DataTable mDT = pageDt.Clone(); mDT.Clear(); //mDT.TableName = mTableName; //mDT = mDT.Clone(); for ( int i = 0; i < mMatches.Length; i++) { mDT.Rows.Add(mMatches[i].ItemArray); } pDS.Tables.Add(mDT); //1 Repeater1.DataSource = GetPagedTable(pageDt, currentPage, PageSize); Repeater1.DataBind(); //2 //Repeater1.DataSource = pDS; //Repeater1.DataBind(); this .TextBox1.Text = strName; //Repeater1.DataSource = GetPagedTable(pageDt, yhdddqi, PageSize); //Repeater1.DataBind(); this .Label1.Text = currentPage.ToString() + "/" + verypage; } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click( object sender, EventArgs e) { verypage = 1; //下一页 if (! string .IsNullOrEmpty(Request.QueryString[ "page" ])) { currentPage = int .Parse(Request.QueryString[ "page" ]); } currentPage++; geovinduDt(); if (currentPage >= pageDt.Rows.Count) { Button1.Enabled = false ; return ; } DataRow[] mMatches = pageDt.Select( "(id=" + currentPage + ")" ); //DataRow matches = new DataRow(); string strName = mMatches[0][ "dd" ].ToString(); this .TextBox1.Text = strName; string answer = string .Empty; if (CheckBoxA.Checked == true ) { answer = "A" ; } if (CheckBoxB.Checked == true ) { if (! string .IsNullOrEmpty(answer)) { answer = answer + "," + "B" ; } else { answer = "B" ; } } if (CheckBoxC.Checked == true ) { if (! string .IsNullOrEmpty(answer)) { answer = answer + "," + "C" ; } else { answer = "C" ; } } if (CheckBoxD.Checked == true ) { if (! string .IsNullOrEmpty(answer)) { answer = answer + "," + "D" ; } else { answer = "D" ; } } Jscript.Alert(answer); this .Label1.Text = currentPage.ToString() + "/" + verypage; Response.Redirect( "DatatablePage.aspx?page=" + (currentPage) + "&name=" + strName); } /// <summary> /// DataTable分页 /// </summary> /// <param name="dt">DataTable</param> /// <param name="PageIndex">页索引,注意:从1开始</param> /// <param name="PageSize">每页大小</param> /// <returns></returns> public static DataTable GetPagedTable(DataTable dt, int currentPageIndex, int pageSize) { //1 //判断当前索引 if (currentPageIndex == 0) return dt; //从数据集合拷贝数据 DataTable newdt = dt.Copy(); //数据清空 newdt.Clear(); //开始数据索引 = 当前页-1 x 每页大小 int rowbegin = (currentPageIndex - 1) * pageSize; //结束数据索引 = 当前页 x 每页大小 int rowend = currentPageIndex * pageSize; //开始数据索引 大于等于 当前数据集合大小 if (rowbegin >= dt.Rows.Count) return newdt; //结束数据索引 大于 当前数据集合大小 if (rowend > dt.Rows.Count) rowend = dt.Rows.Count; //遍历数据 for ( int i = rowbegin; i <= rowend - 1; i++) { DataRow newdr = newdt.NewRow(); DataRow dr = dt.Rows[i]; foreach (DataColumn column in dt.Columns) { newdr[column.ColumnName] = dr[column.ColumnName]; } newdt.Rows.Add(newdr); } return newdt; //2 //if (currentPageIndex == 0) //{ //return dt; //} //DataTable newdt = dt.Clone();// dt.Copy(); //int rowbegin = (currentPageIndex - 1) * pageSize;//当前页的第一条数据在dt中的位置 //int rowend = currentPageIndex * pageSize;//当前页的最后一条数据在dt中的位置 //if (rowbegin >= dt.Rows.Count) //{ // return newdt; //} //if (rowend > dt.Rows.Count) //{ // rowend = dt.Rows.Count; //} //DataView dv = dt.DefaultView; //for (int i = rowbegin; i <= rowend - 1; i++) //{ // newdt.ImportRow(dv[i].Row); //} //return newdt; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <%@ Page Language= "C#" AutoEventWireup= "true" CodeBehind= "DatatablePage.aspx.cs" Inherits= "ShoppingDeals.Admin.VipHKExamSystem.DatatablePage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head runat= "server" ><meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title>Datatable paging,Repeater with Paging </title> <meta name= "author" content= "塗聚文" /> </head> <body> <form id= "form1" runat= "server" > <div> <table style= "width:100%;" > <tbody> <tr> <td>id</td> <td>name:</td> </tr> <asp:Repeater ID= "Repeater1" runat= "server" > <ItemTemplate> <tr> <td><%# Eval( "ddno" ).ToString()%></td> <td><%# Eval( "dd" ).ToString()%></td> </tr> </ItemTemplate> </asp:Repeater> </tbody> </table> <div><b>選擇答案:</b><asp:CheckBox ID= "CheckBoxA" runat= "server" Text= "A" /><asp:CheckBox ID= "CheckBoxB" runat= "server" Text= "B" /><asp:CheckBox ID= "CheckBoxC" runat= "server" Text= "C" /><asp:CheckBox ID= "CheckBoxD" runat= "server" Text= "D" /></div> <asp:TextBox ID= "TextBox1" runat= "server" ></asp:TextBox> <asp:Button ID= "Button1" runat= "server" Text= "下一頁" OnClick= "Button1_Click" /> <asp:Label ID= "Label1" runat= "server" Text= "Label" ></asp:Label></div> </form> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | public partial class ExamCNTestDisplay : System.Web.UI.Page { VIPExamQuestionsBLL vIPExamQuestionsBLL = new VIPExamQuestionsBLL(); VipExamProjectBLL vipExamProjectBLL = new VipExamProjectBLL(); VipExamProjectInfo vipExamProjectInfo = new VipExamProjectInfo(); string examProjectId = string .Empty; int fcount = 0; int _currentPage = 0; int k = 0; /// <summary> /// /// </summary> public int currentPage { set { _currentPage = value; } get { return _currentPage; } } public int getPage = 1; DataTable Tissues = new DataTable(); /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load( object sender, EventArgs e) { //DataTable Tissues = (DataTable)Session["Tissues"]; if (Tissues.Rows.Count==0) { Tissues.Columns.Add( "ID" , typeof ( int )); Tissues.Columns.Add( "Answer" , typeof ( string )); } if (!IsPostBack) { examProjectId = Request.QueryString[ "DisID" ]; if (k == 0) { ViewState[ "ExamProjectId" ] = Request.QueryString[ "DisID" ]; k++; } else { examProjectId = ViewState[ "ExamProjectId" ].ToString(); k++; } ////Session["BackLogin"] = "ExamProjectList.aspx"; //if (object.Equals(Session["ExamAdminLogin"], null)) //{ // Jscript.AlertAndRedirect("沒有登錄無權限!", "VIPExamLogin.aspx"); //} if (! string .IsNullOrEmpty(examProjectId)) { vipExamProjectInfo = vipExamProjectBLL.SelectVipExamProject( int .Parse(examProjectId)); if (! object .Equals(vipExamProjectInfo, null )) { Page.Title = vipExamProjectInfo.ExamProjectTitle; } PagedDataSource pds = new PagedDataSource(); if (! object .Equals(vIPExamQuestionsBLL.SelectVipQuestionsExamProjectDisplay( int .Parse(examProjectId)), null )) { pds.DataSource = getdatatable(vIPExamQuestionsBLL.SelectVipQuestionsExamProjectDisplay( int .Parse(examProjectId))).DefaultView; //pds.AllowCustomPaging = true; pds.AllowPaging = true ; //pds.AllowServerPaging = true; pds.PageSize = 1; fcount = pds.Count; currentPage = Convert.ToInt32(Request[ "page" ]); //设当前页 pds.CurrentPageIndex = currentPage; getPage = currentPage; FormView1.DataSource = pds; FormView1.DataBind(); //设几个超链接 if (!pds.IsFirstPage) { lnkUp.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1); //LinkButton1.ResolveClientUrl(Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1)); } if (!pds.IsLastPage) { //LinkButton1.ResolveClientUrl (Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1)); lnkDown.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1) + "&DisID=" + examProjectId; ((Label)FormView1.Controls[0].FindControl( "Label3" )).Text = (currentPage + 1).ToString(); //bool ca = CheckBoxA.Checked;CheckBoxA //bool cb = CheckBoxB.Checked; //bool cc = CheckBoxC.Checked; //bool cd = CheckBoxD.Checked; //FormView1.DefaultMode = FormViewMode.Insert; CheckBox CheckBoxA = (CheckBox)FormView1.Controls[0].FindControl( "CheckBoxA" ); //(CheckBox)FormView1.Row.FindControl("CheckBoxA");// CheckBox CheckBoxB = (CheckBox)FormView1.Controls[0].FindControl( "CheckBoxB" ); CheckBox CheckBoxC = (CheckBox)FormView1.Controls[0].FindControl( "CheckBoxC" ); CheckBox CheckBoxD = (CheckBox)FormView1.Controls[0].FindControl( "CheckBoxD" ); HtmlInputCheckBox CheckBox1 = (HtmlInputCheckBox)FormView1.Controls[0].FindControl( "CheckBox1" ); string answer = string .Empty; if (FormView1.CurrentMode == FormViewMode.ReadOnly) { // Jscript.Alert(CheckBoxB.Checked.ToString()); if (CheckBoxA.Checked == true ) { answer = "A" ; } if (CheckBoxB.Checked == true ) { if (! string .IsNullOrEmpty(answer)) { answer = answer + "," + "B" ; } else { answer = "B" ; } } if (CheckBoxC.Checked == true ) { if (! string .IsNullOrEmpty(answer)) { answer = answer + "," + "C" ; } else { answer = "C" ; } } if (CheckBoxD.Checked == true ) { if (! string .IsNullOrEmpty(answer)) { answer = answer + "," + "D" ; } else { answer = "D" ; } } } //if (((CheckBox)FormView1.FindControl("CheckBoxA")).Checked == true) //{ // Jscript.Alert("ok"); //} string id = ((Label)FormView1.Controls[0].FindControl( "Label2" )).Text; Tissues.Rows.Add( int .Parse(id), answer); Jscript.Alert(id + "," + answer + "," + CheckBox1.Checked.ToString()); CheckBoxA.Checked = false ; CheckBoxB.Checked = false ; CheckBoxC.Checked = false ; CheckBoxD.Checked = false ; } else { ((Label)FormView1.Controls[0].FindControl( "Label3" )).Text = (currentPage + 1).ToString(); lnkDown.Visible = false ; //顯示完不顯示,而顯示提交按鈕 this .Button1.Visible = true ; // Jscript.Alert("end"); Session.Add( "Tissues" , Tissues); //(DataTable)Session["Tissues"] = Tissues; string id = ((Label)FormView1.Controls[0].FindControl( "Label2" )).Text; if (Tissues.Rows.Count > 0) { Jscript.Alert(Tissues.Rows[0][0].ToString()); } } } else { Jscript.Alert( "還沒有添加試題" ); return ; } // FormView1.DataSource = vIPExamQuestionsBLL.SelectVipQuestionsExamProjectDisplay(1); // FormView1.DataBind(); } else { Jscript.Alert( "沒有查找相關關項目的數據!" ); return ; } } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void FormView1_PageIndexChanging( object sender, FormViewPageEventArgs e) { //FormView1.DataSource=getdatatable(vIPExamQuestionsBLL.SelectVipQuestionsExamProjectDisplay(int.Parse(examProjectId))).DefaultView; //FormView1.DataBind(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click( object sender, EventArgs e) { if (FormView1.PageIndex == fcount) { Jscript.Alert( "end" ); DataTable table = Session[ "Tissues" ] as DataTable; if (table.Rows.Count>0) { //read table and push in Sqlserver Jscript.Alert(table.Rows[0][0].ToString()); } else { //session either expired or invalid page being accessed. } } } /// <summary> /// /// </summary> /// <param name="dt"></param> /// <returns></returns> private DataTable getdatatable(DataTable table) { if (! object .Equals(table, null )) // { DataTable tableDec = new DataTable(); #region ExamQuestionsProject,ExamQuestionsTitle,ExamQuestionsA,ExamQuestionsB,ExamQuestionsC,ExamQuestionsD,ExamQuestionsAnswer,ExamQuestionsDesc,ExamProjectTitle tableDec.Columns.Add( "ExamQuestionsID" , typeof ( int )); tableDec.Columns.Add( "ExamQuestionsProject" , typeof ( int )); tableDec.Columns.Add( "ExamQuestionsTitle" , typeof ( string )); tableDec.Columns.Add( "ExamQuestionsA" , typeof ( string )); tableDec.Columns.Add( "ExamQuestionsB" , typeof ( string )); tableDec.Columns.Add( "ExamQuestionsC" , typeof ( string )); //在職狀態 tableDec.Columns.Add( "ExamQuestionsD" , typeof ( string )); tableDec.Columns.Add( "ExamQuestionsAnswer" , typeof ( string )); tableDec.Columns.Add( "ExamQuestionsDesc" , typeof ( string )); tableDec.Columns.Add( "ExamProjectTitle" , typeof ( string )); tableDec.Columns.Add( "ExamProjectCheck" , typeof ( bool )); #endregion foreach (DataRow row in table.Rows) // Loop over the rows. { bool check = false ; tableDec.Rows.Add(row[ "ExamQuestionsID" ], row[ "ExamQuestionsProject" ], row[ "ExamQuestionsTitle" ], row[ "ExamQuestionsA" ], row[ "ExamQuestionsB" ], row[ "ExamQuestionsC" ], row[ "ExamQuestionsD" ], row[ "ExamQuestionsAnswer" ], row[ "ExamQuestionsDesc" ], row[ "ExamProjectTitle" ], check); } return tableDec; } else { return null ; } //DataColumn col = new DataColumn("check", typeof(bool)); //dt.Columns.Add(col); ////dt.Columns.AddRange(col); ////dt.Columns.AddRange("check", typeof(bool)); //for (int i=0;i<dt.Rows.Count;i++) //{ // DataRow row = dt.NewRow(); // row["check"] = false; // dt.Rows.Add(row); //} ////for(int i=0;i<dt.Rows.Count;i++) ////{ ////} //return dt; } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2012-02-27 javascript UTF-8的繁体与简体转换