Repeater控件怎么实现排序?给出详细的后台代码
- 提问者网友:容嬷嬷拿针来
- 2021-05-23 05:19
- 五星知识达人网友:煞尾
- 2021-05-23 06:49
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
public partial class RepeaterTest : System.Web.UI.Page
{
private static SqlConnection connection;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Page"] = 0;
DataBind();
}
}
private void DataBind()
{
if (Pager != 0)
{
int c = Pager * 5;
SqlDataSource1.SelectCommand = "select top 5 * from PetDiary where diary_id not in (select top " + c + " diary_id from PetDiary) order by diary_id";
}
else
SqlDataSource1.SelectCommand = "select top 5 * from PetDiary ";
if (Pager == 0)
btnPre.Enabled = false;
else
btnPre.Enabled = true;
string sql = "select count(*) from PetDiary";
SqlCommand cmd = new SqlCommand(sql, Connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
int total;
total = result / 5;
if (Pager== total)
btnNext.Enabled = false;
else
btnNext.Enabled = true;
lblCurrentPage.Text = "第" + (Pager + 1).ToString() + "页" + "||共" + (total+1) + "页";
Repeater1.DataSource = SqlDataSource1;
Repeater1.DataBind();
}
private int Pager
{
get
{
return (int)ViewState["Page"];
}
set
{
ViewState["Page"] = value;
}
}
protected void btnPre_Click(object sender, EventArgs e)
{
Pager--;
DataBind();
}
protected void btnNext_Click(object sender, EventArgs e)
{
Pager++;
DataBind();
}
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["epetConnectionString3"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
}