Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate table dynamically and achieve paging effect

Shulou Source: shulou.com Published: 2022-06-03 05:41:24 11月17日 Update

This article will explain in detail how to dynamically generate table and achieve the paging effect. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Foreground code:

The copy code is as follows:

Search word:

Background code:

The copy code is as follows:

Protected void Page_Load (object sender, EventArgs e)

{

If (! IsPostBack)

{

ViewState ["pageIndex"] = 1

DataPage ()

}

}

Private void dataPage ()

{

String constr = @ "data source=.;initial catalog=News;user id=sa;password=111111;"

SqlConnection conn = new SqlConnection (constr)

Conn.Open ()

SqlCommand cmd = conn.CreateCommand ()

# region uses stored procedures to achieve data paging

/ / cmd.CommandType = CommandType.StoredProcedure

/ / cmd.CommandText = "Pro_SelectNewsPage"

/ / cmd.Parameters.AddWithValue ("@ pageIndex", Convert.ToInt32 (ViewState ["pageIndex"]))

/ / cmd.Parameters.AddWithValue ("@ pageSize", 3)

/ / SqlParameter parm = new SqlParameter ("@ count", SqlDbType.Int)

/ / cmd.Parameters.Add (parm)

/ / parm.Direction = ParameterDirection.Output

# endregion

# region passes the SQl statement to achieve paging and displays two records by default for search paging

Cmd.CommandType = CommandType.Text

Cmd.CommandText = "select top 2 * from (select * from T_News where NewsTitle like @ title) as t where t.Id not in (select top ((@ pageIndex-1) * 2) t.Id from (select * from T_News where NewsTitle like @ title) as t) order by t.Id"

Cmd.Parameters.AddWithValue ("@ pageIndex", Convert.ToInt32 (ViewState ["pageIndex"]))

Cmd.Parameters.AddWithValue ("@ title", "%" + TextBox1.Text.Trim () + "%")

# endregion

SqlDataAdapter adapter = new SqlDataAdapter (cmd)

DataTable dt = new DataTable ()

Adapter.Fill (dt)

/ / ViewState ["pageCount"] = parm.Value

Cmd.CommandText = "select count (*) from T_News where NewsTitle like @ titles"

Cmd.Parameters.AddWithValue ("@ titles", "%" + TextBox1.Text.Trim () + "%")

Int sum = Convert.ToInt32 (cmd.ExecuteScalar ())

If (sum% 2 = = 0)

{

ViewState ["pageCount"] = sum/2

}

Else

{

ViewState ["pageCount"] = sum / 2 + 1

}

Conn.Close ()

Conn.Dispose ()

StringBuilder sb = new StringBuilder ()

Sb.Append ("")

Foreach (DataRow row in dt.Rows)

{

Sb.Append ("" + row ["NewsTitle"] .ToString () + "")

Sb.Append ("" + row ["NewsContent"] .ToString () + "")

Sb.Append ("" + row ["CreateTime"] .ToString () + "")

}

Sb.Append ("")

Divmain.InnerHtml = sb.ToString ()

LabCountPage.Text = "Total" + ViewState ["pageCount"] + "Page"

Labpage.Text = "current first" + ViewState ["pageIndex"] + "Page"

}

Protected void Button1_Click (object sender, EventArgs e)

{

ViewState ["pageIndex"] = "1"

DataPage ()

}

Protected void btnFirst_Click (object sender, EventArgs e)

{

ViewState ["pageIndex"] = "1"

DataPage ()

}

Protected void btnPro_Click (object sender, EventArgs e)

{

Int index = Convert.ToInt32 (ViewState ["pageIndex"])

Index--

If (index > = 1)

{

ViewState ["pageIndex"] = index

DataPage ()

}

}

Protected void btnNext_Click (object sender, EventArgs e)

{

Int index = Convert.ToInt32 (ViewState ["pageIndex"])

Index++

If (index 0 & & I)

Tags: Code effects articles dynamic generation more search good practical content foreground background data articles knowledge statements procedures references storage help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple MySQL Microsoft NVidia