日期:2014-05-19  浏览次数:20514 次

如何用Label来做新闻详细页?
下面是用label1显示的一个返回首行首列的实例.我想用更多的label来显示详细的查询到的内容该如何改下面的代码?

刚开始入门学习.NET,请回答问题的朋友能尽量说得详细一些,谢谢.


public   partial   class   showNewsDetails   :   System.Web.UI.Page
{
        private   string   newID;
        public   string   NewID
        {
                set
                {
                        this.newID   =   value;
                }
        }
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                SqlConnection   con   =   DB.createCon();
                SqlCommand   cmd   =   new   SqlCommand( "select   *   from   newsMaster   where   newsID= ' "+newID+ " ' ",   con);
                con.Open();
                string   strID   =   Request.QueryString[ "newsID "].ToString();  
                cmd.CommandText   =   "select   *   from   newsMaster   where   newsID= ' "   +   strID   +   " ' ";
                string   newsTitle   =   Convert.ToString(cmd.ExecuteScalar());
                this.Label1.Text   =   newsTitle;
        }
}

------解决方案--------------------
第一:你为什么要使用Label来显示你的新闻标题啊?你要更多的不是列表吗?
还是使用DataList或者DataGrid方便;
你一定要使用的话,就是你上面的代码拷贝几遍就好了,不过你要考虑你的newID怎么去取到
------解决方案--------------------
用Repeater 包个label1 然后用数据集绑定Repeater

具体做法你可以去搜有关Repeater控件的说明
------解决方案--------------------
SqlCommand cmd = new SqlCommand( "select upDateTime from newsMaster where newsID= ' " + strID + " ' ", con);
SqlDataReader dr = cmd.ExecuteReader();

dr.Read();
this.TextBox1.Text = dr[ "newsTitle "].ToString();
this.TextBox2.Text = dr[ "upDateTime "].ToString();
dr.Close

==========================
楼主领会一下
------解决方案--------------------
楼上正解,用SqlDataReader不就好了,干吗要每次取首行首列呢。