62,046
社区成员
发帖
与我相关
我的任务
分享
上一篇:<a href='<%# preurl %>'><%# pretitle %></a>
//上一篇,下一篇
protected void pre_next()
{
int now_id;
try
{
now_id = Convert.ToInt32(Request.QueryString["id"].ToString());
}
catch
{
now_id = 0;
}
string pre_Sql = "select top 1 title,max(id) from news where id < now_id";
string next_Sql = "select top 1 title,min(id) from news where id > now_id";
cn.Open();
SqlCommand cmd1 = new SqlCommand(pre_Sql, cn);
SqlDataReader sdr1 = cmd1.ExecuteReader();
if (sdr1.Read())
{
pretitle = sdr1["title"].ToString().Trim();
preurl = "Article.aspx?id=" + sdr1["id"].ToString() + " ";
}
else
{
pretitle = "第一篇了。";
}
cmd1.Dispose();
sdr1.Close();
SqlCommand cmd2 = new SqlCommand(next_Sql, cn);
SqlDataReader sdr2 = cmd2.ExecuteReader();
if (sdr2.Read())
{
nexttitle = sdr2["title"].ToString().Trim();
nexturl = "Article.aspx?id=" + sdr2["id"].ToString() + " ";
}
else
{
nexttitle = "最后一篇了";
}
sdr2.Close();
cmd2.Dispose();
cn.Close();
}
<form id="form1" runat="server">
<div>
<a id="A1" runat="server">
<%# pretitle %></a>
</div>
</form>
public string pretitle = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
pretitle = "标题";
A1.DataBind();
}
//Page_Load外面定义了四个变量,一个数据库链接:
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["new_conn"].ConnectionString);
//这个连接没错的, 一直都在用它.
public string pretitle;
public string preurl;
public string nexttitle;
public string nexturl;