1: Page picture display
2: Layout picture display. DataGrid control layout by itself
I still attach it, haha, it's more convenient
<tr valign="top">
<td valign="middle" colspan="2">
<table cellspacing="0" cellpadding="0" width="100%" align="center" border="0" style="height:30px">
<tr valign="top">
<td valign="middle" align="left">
<asp:Label ID="pageInfo" runat="server">共0条,分1页,第1页</asp:Label>
其中每页:<asp:TextBox
ID="PageSize" runat="server" Text="30" Width="40px"></asp:TextBox>条记录
</td>
<td align="right" valign="middle">
<asp:LinkButton ID="Linkbutton1" runat="server" ForeColor="#218fee"
οnclick="Linkbutton1_Click" >首 页</asp:LinkButton>
<asp:LinkButton ID="Linkbutton2" runat="server" ForeColor="#218fee"
οnclick="Linkbutton1_Click" >上一页</asp:LinkButton>
<asp:LinkButton ID="Linkbutton3" runat="server" ForeColor="#218fee"
οnclick="Linkbutton1_Click" >下一页</asp:LinkButton>
<asp:LinkButton ID="Linkbutton4" runat="server" ForeColor="#218fee"
οnclick="Linkbutton1_Click" >尾 页</asp:LinkButton>
跳转<input id="newpage" type="text" size="4" name="newpage" runat="server"/>页
<asp:LinkButton ID="Linkbutton5" runat="server" ForeColor="#218fee"
οnclick="Linkbutton1_Click" >GO</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
3: Click event code for pagination
protected void Linkbutton1_Click(object sender, EventArgs e)
{
int nIndex2 = -1;
LinkButton b = null;
if (sender.GetType() != typeof(LinkButton)) return;
b = sender as LinkButton;
int nCurrentPageIndex = 1;
int nPageCount = 0;
string str1 = ViewState["CurPageIndex"] != null ? ViewState["CurPageIndex"].ToString() : "";
string str2 = ViewState["PageCount"] != null ? ViewState["PageCount"].ToString() : "";
if (str1 == "" || !int.TryParse(str1, out nCurrentPageIndex)) nCurrentPageIndex = 1;
if (str2 == "" || !int.TryParse(str2, out nPageCount)) nPageCount = 1;
switch (b.ID)
{ case "Linkbutton1": //Locate the first page newpage.Value = ""; nIndex2 = 1; break; case "Linkbutton2": //locate the previous page newpage.Value = ""; nIndex2 = nCurrentPageIndex-1; if (nIndex2 == 0) nIndex2 = 1; break; case "Linkbutton3": //Locate the next page newpage.Value = ""; nIndex2 = nCurrentPageIndex + 1; if (nIndex2 >= nPageCount + 1 ) nIndex2 = nPageCount; break;
case "Linkbutton4": //定位最末页
newpage.Value = "";
nIndex2 = nPageCount;
break;
case "Linkbutton5"://导航到某一页
if (!int.TryParse(newpage.Value.Trim(), out nIndex2)) nIndex2 = nCurrentPageIndex;
if (nIndex2 > nPageCount) nIndex2 = nPageCount;
if (nIndex2 == 0)
{
newpage.Value = "1";
nIndex2 = 1;
}
break;
default:
nIndex2 = nCurrentPageIndex;
break;
}
if (nIndex2 <0) return;
GetPageIndex(nIndex2); //Get the paged data
}
private void GetPageIndex(int pageIndex)
{ pageInfo.Text = "Page 1/10 pages in total, 15 records per page in total 150 records"; newpage.Value = "0";
//The following is the official code
int nPageSize = 30; //How many items are displayed per page
int nCurPageIndex = 0;
int nPageCount = 0; //How many pages are in total
int nPageIndex = pageIndex;//How many pages are
int nRecordSetCount = 0; / /How many records are in total
string strPageInfo = "Page {2}/Page {1}, Total {0} records, {3} per page";
string strErrMsg = "";
string strFilter = GetFileter();
string strSort = ViewState["SortString"] != null? ViewState["SortString"].ToString(): "";
if (PageSize.Text.Trim() != "")
{
if (!int.TryParse(PageSize.Text.Trim(), out nPageSize)) nPageSize = 30;
}
PageSize.Text = nPageSize.ToString();
DbHelper db= new DbHelper();
DataTable dt = db.GetProjectBasicTable(strFilter, strSort, nPageSize, ref nPageIndex, ref nRecordSetCount, ref nPageCount, out strErrMsg);
if (dt == null)
{
(Context.ApplicationInstance as Global).MessageBox(strErrMsg);
return;
}
nCurPageIndex = nPageIndex;//当前页数
//显示分页信息
pageInfo.Text = String.Format(strPageInfo, nRecordSetCount, nPageCount, nPageIndex, nPageSize);
if (nPageIndex <= nPageCount) newpage.Value = nPageIndex.ToString();
else newpage.Value = "0";
ViewState["CurPageIndex"] = nCurPageIndex.ToString();//Current page
ViewState["PageCount"] = nPageCount.ToString();//Number of all pages
. . . . . . The following are some program judgments that I want to achieve
}
4: DbHelper class
public DataTable GetProjectBasicTable(string strFilter, string strSort, int nPageSize, ref int nPageIndex, ref int nRecordSetCount, ref int nPageCount, out string strErrMsg)
{
//It depends on the individual program code, here I need to judge whether to log in
if (HttpContext.Current.Session["QciWeb"] == null)
{ strErrMsg = "The session was accidentally lost! Please log in again."; return null; } strErrMsg = ""; nRecordSetCount = 0; nPageCount = 0; SqlConnection conn = null; SqlCommand comm = null; SqlDataAdapter da = null; DataTable dt = null; object objResult = null; int nFirstIndex = 0;
//Here is the connection string of web.config to the database
string strConnection = WebConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
string strSQL1 = @" Query the total number of all sql data (count) {0} ";
string strSQL2 = @" Query all sql data {0}{1} ";
if (strSort != "") strSort = "order by " + strSort;
else strSort = "order by 时间排序 desc";
strSQL1 = string.Format(strSQL1, strFilter);
strSQL2 = string.Format(strSQL2, strFilter, strSort);
try
{
conn = new SqlConnection(strConnection);
conn.Open();
comm = new SqlCommand(strSQL1, conn);
objResult = comm.ExecuteScalar();
nRecordSetCount = int.Parse(objResult.ToString());
//Fix the total number of pages
nPageCount = nRecordSetCount / nPageSize;
if (nPageCount * nPageSize <nRecordSetCount) nPageCount++;
//修正开始页数
if (nPageIndex > nPageCount) nPageIndex = nPageCount;
else if (nPageIndex == 0) nPageIndex = 1;
//Revise the number of starting records
nFirstIndex = (nPageIndex-1) * nPageSize;
if (nFirstIndex <0) nFirstIndex = 0;
dt = new DataTable();
da = new SqlDataAdapter(comm);
da.SelectCommand.CommandText = strSQL2;
da.Fill(dt);
return dt;
}
catch (Exception oe)
{
strErrMsg = oe.Message;
return null;
}
finally
{
if (dt != null) dt.Dispose();
if (comm != null) comm.Dispose();
if (da != null) da.Dispose();
if (conn != null)
{
if (conn.State == ConnectionState.Open) conn.Close();
conn.Dispose();
}
da = null;
conn = null;
dt = null;
comm = null;
}
}
5: Running effect display
The paging code is relatively old-style, so let's see it.