datalist 模板列绑定数据

来源:百度知道 编辑:UC知道 时间:2024/09/24 16:31:32
aspx界面
[作者为:<%#DataBinder.Eval(Container.DataItem, "BG_n_author")%>] //[人气为:<%#DataBinder.Eval(Container.DataItem, "BG_n_hit")%>]//

cs界面
protected void Page_Load(object sender, EventArgs e)
{
string strconn = ConfigurationManager.AppSettings["conn"];
SqlConnection cn = new SqlConnection(strconn);
cn.Open();

SqlCommand selcetcmd = new SqlCommand("select BG_n_author FROM BG_news",cn);

SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selcetcmd;

DataSet custDS = new DataSet();
custDA.Fill(custDS, "BG_news");
DataList2.DataSource =new DataView(custDS.Tables[0]);
DataList2.DataBind();
cn.Close();

问题我如果只帮定一个数据,也就是[作者为:<%#DataBinder.Eval(Container.DataItem, "BG_n_author")%>

他们的答案都没对,我把我的答案发给你了

你的select语句错了。
select BG_n_author,BG_n_hit from BG_news
前台绑定项跟后台数据源是一一对应的。也就是DataList2.DataSource中含有BG_n_hit列,你才能在前台中使用<%#DataBinder.Eval(Container.DataItem, "BG_n_hit")%>

连接不是有了吗?
SqlCommand selcetcmd = new SqlCommand("select BG_n_author FROM BG_news",cn);
cn就是连接
而且你用的是dataset,不需要使用cn.open()和cn.close()

SqlCommand selcetcmd = new SqlCommand("select BG_n_author,BG_n_hit FROM BG_news",cn);

SqlCommand selcetcmd = new SqlCommand("select * FROM BG_news",cn);

加个通用...直接

你查询"BG_n_hit"字段了吗,我没有看到类似"select BG_n_hit from 表"语句,还有就是你没有执行查询,加一个CustDA.SelectCommand.E?????后面我忘了,对不住啊