关于Datalist的几个问题,求坛友帮助。
在做一个新闻网站,遇见几个难点。
1.目的:DataList中放一个Label,显示文章评论数量.
(1)显示评论数量,存储过程我是这样写的:Select count(comment.cid) from comment,news where comment.nid=news.nid;
但取出来的东西,怎么给Label赋值,for循环??
2.目的:分页显示并控制每页Datalist显示的数量.
(2)
int pageCount;//总页数
int currentPage = 1;//第定义当前页
void bind()
{
string str = @"Data Source=.\sqlexpress;Initial Catalog=News;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
string sqlStr = "select * from News";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "nid");
//创建数据源
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["nid"].DefaultView;
//允许分页
pds.AllowPaging = true;
//设置每页显示记录数
pds.PageSize = int.Parse(this.DropDownList1.SelectedItem.Value);
//获取总页数
pageCount = pds.PageCount;
this.Label1.Text = pageCount.ToString();
pds.CurrentPageIndex = currentPage - 1;
//当前页
this.Label2.Text = Convert.ToString(currentPage);
this.DataList1.DataSource = pds;
this.DataList1.DataBind();