实现类似gridview的功能一
之前实现分页的时候因为懒得写都是用sqldatasorse和gridview,里面在写点自己需要的sql语句就可以实现了
下午的时候看到之前的就自己写了一个分页,里面包括点击“详细信息”连接到相应页面
因主要实现的是没行的超连接功能,只需要一个存储过程,用于取值实现每行的超连接传值。
以下是存储过程
CREATE PROCEDURE Pr_getPara
(
@para int//第几页的第几行在数据库中是第几条记录
)
AS
declare @str varchar(500)
set
@str=\’select top 1 id,name from sample where id
not in (select top \’+str(@para)+\’ id from sample order by id desc) order by id desc\’
exec (@str)
GO
实现功能如图(上传几次图片都有问题,不知道为什么,不能显示)
后台代码如下:
static int rowCnt;//当前页记录的数目
protected void Page_Load(object sender, EventArgs e)
{
string pageindex;
//判断第几页
if (Request.QueryString[“page”] == null)
{
lbPage.Text = “1”;
pageindex = “1”;
}
else
{
lbPage.Text = Request.QueryString[“page”].ToString();
pageindex = Request.QueryString[“page”].ToString();
}
//如果没有下一页则隐藏“后一页”
string str = “select count(*) as count_id from sample”;
DataSet ds_count = SqlBase.ExecuteSql4Ds(str);
DataRow row_count = ds_count.Tables[0].Rows[0];
int i = Int32.Parse(row_count[“count_id”].ToString());//数据总数
int now_page = Int32.Parse(lbPage.Text);
if (i < (now_page * 10))
{
lbtnNext.Visible = false;
}
if (i / 10 < now_page)
rowCnt = i – (now_page – 1) * 10;
else
rowCnt = 10;
if (lbPage.Text == “1”)
lbtnPre.Visible = false;
int rowCount = 0;
for (rowCount=0; rowCount < rowCnt; rowCount++)
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[“CONNECTIONSTRING”].ToString());
string paraNO = Convert.ToString(Convert.ToInt32(Convert.ToDouble(pageindex)-1) * 10 + rowCount);
SqlCommand cmdPara = new SqlCommand();
cmdPara.Parameters.Add(“@para”,SqlDbType.Int,4,paraNO);
cmdPara.Parameters[“@para”].Value = Convert.ToString(paraNO);
cmdPara.Connection = conn;
cmdPara.CommandText = “Pr_getPara”;
cmdPara.CommandType = CommandType.StoredProcedure;
//conn.Open();
DataSet dsPara = new DataSet();
SqlDataAdapter adapterPara = new SqlDataAdapter(cmdPara);
adapterPara.Fill(dsPara,”sample”);
DataRow drPara = dsPara.Tables[0].Rows[0];
TableRow tRow = new TableRow();
Table1.Controls.Add(tRow);
TableCell tCell = new TableCell();
tRow.Controls.Add(tCell);
System.Web.UI.WebControls.Label lb = new Label();
string s = “a.aspx?ID=”+drPara[“id”].ToString();
lb.Text = ““+drPara[“name”].ToString()+”“;
tCell.Controls.Add(lb);
}
}
protected void lbtnPre_Click(object sender, EventArgs e)
{
int next_page = Int32.Parse(lbPage.Text) – 1;
Response.Redirect(“my.aspx?page=” + next_page);
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
string next_page = Convert.ToString(Int32.Parse(lbPage.Text) + 1);
Response.Redirect(“my.aspx?page=” + next_page);
}
这里就已经实现了和gridview里的hyperlink一样的功能了