Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate table dynamically and achieve paging effect

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report