| pnlPager.Controls.Add(getLinkButton("Lst", "末页", "pgBtn", pageCount.ToString())); return pnlPager; } LinkButton getLinkButton(string lbId, string lbText, string lbCmdName, string lbCmdArg) { LinkButton lb = new LinkButton(); lb.ID = lbCmdName + lbId; lb.Text = lbText; lb.CommandName = lbCmdName; lb.CommandArgument = lbCmdArg; lb.Command += new CommandEventHandler(pageTurn); return lb; } void pageTurn(object sender, CommandEventArgs e) { LinkButton lb = (LinkButton)sender; int selectedPage = int.Parse(lb.CommandArgument); int firstPage = selectedPage / pages * pages; firstPage = (selectedPage % pages == 0) ? (firstPage - pages) : firstPage; hidFirstPage.Value = firstPage.ToString(); dgBind(getSqlString(selectedPage)); pnlButtons.Controls.RemoveAt(pnlButtons.Controls.Count - 1); pnlButtons.Controls.Add(getPager(firstPage)); lb = (LinkButton)pnlButtons.Controls[pnlButtons.Controls.Count - 1].FindControl("pgBtn" + selectedPage.ToString()); lb.Enabled = false; } void dgBind(string sqlStr) { SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn); DataTable dt = new DataTable(); conn.Open(); da.Fill(dt); conn.Close(); da.Dispose(); dg.DataSource = dt; dg.DataBind(); } void dg_ItemDataBound(object sender, DataGridItemEventArgs e) { dg.Controls[0].EnableViewState = false; } |