简体   繁体   English

将数据库中的最新消息显示到我的网站上的问题

[英]issues displaying latest news from database onto my website

I am having problems in setting a up a latest news panel on my website. 我在网站上设置最新的新闻面板时遇到问题。

Currently 目前

public System.Data.SqlClient.SqlConnection Admin_conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = News();
    if (dt.Rows.Count > 0) // Check if the DataTable returns any data from database
    {

        lbltest.Text = dt.Rows[0]["NewsTitle"].ToString();
        lblDate.Text = dt.Rows[0]["NewsDate"].ToString();
        lbldescription.Text = dt.Rows[0]["NewsDescription"].ToString();
    }
}


protected DataTable News()
{
    DataTable dt = new DataTable();
    SqlDataAdapter data = new SqlDataAdapter("SELECT NewsTitle, NewsDescription, NewsDate FROM News WHERE [NewsDate] < getdate()", Admin_conn);
    data.Fill(dt);
    return dt;

}

But the above code just display news of single row.. I want to display all the news on my website. 但是上面的代码仅显示单行新闻。.我想在我的网站上显示所有新闻。 Which control should I use to display all the records from the database in a proper order like news title, news description then the next news title and description..... 我应该使用哪个控件以适当的顺序显示数据库中的所有记录,例如新闻标题,新闻描述,然后是下一个新闻标题和描述。

Is there any way to use ajax accordion so that all the news title will be displayed and when I click on the specific news title, the description of that news will be displayed. 有什么方法可以使用ajax手风琴来显示所有新闻标题,当我单击特定新闻标题时,将显示该新闻的描述。

Any suggestions or tutorial will be highly appreciated.. 任何建议或教程将不胜感激。

I believe you are looking for the ListView Control. 我相信您正在寻找ListView控件。

It will allow you to bind to the entire set of rows being returned from the database and provide a template for each item. 它将允许您绑定到数据库返回的整个行集合,并为每个项目提供一个模板。

You can also take a few minutes to watch this video tutorial from Microsoft: 您也可以花几分钟时间观看Microsoft提供的此视频教程:

The ListView Control: The Official Microsoft ASP.NET Site ListView控件:官方Microsoft ASP.NET网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM