简体   繁体   English

从SQL数据库检索动态内容并将其显示到ASP.NET页中

[英]Retrieving and displaying dynamic content from SQL database into ASP.NET pages

I am currently working on the development of news portal website in asp.net. 我目前正在asp.net中开发新闻门户网站。 I have created a table in sql server 2008 with parameters (NewsId, NewsTitle, NewsDetails, NewsCategory). 我已经在SQL Server 2008中用参数(NewsId,NewsTitle,NewsDetails,NewsCategory)创建了一个表。 I am now able to dynamically retrieve the NewsTitle from the database and displaying it in my aspx page. 现在,我可以从数据库中动态检索NewsTitle并将其显示在我的aspx页面中。 Now I want to pass the NewsId for that particular NewsTitle and then retrieve the NewsDetails in a separate aspx page. 现在,我想传递该特定NewsTitle的NewsId,然后在单独的aspx页面中检索NewsDetails。 What is the best thing to do this in ASP.NET? 在ASP.NET中最好的做法是什么? I don't want to create new aspx pages every time a link to the NewsTitle is clicked, rather I am interested to use the same Details page and pass different ID parameters? 我不想每次单击NewsTitle的链接时都创建新的aspx页面,而是我想使用相同的Details页面并传递不同的ID参数?

regards, 问候,

I'd keep them both on the same page and employ a master-details pattern using something like panels to toggle the visibility: 我将它们都放在同一页面上,并使用类似面板的主从样式来切换可见性:

http://leedumond.com/blog/master-detail-editing-inserting-deleting-with-a-listview-and-detailsview/ http://leedumond.com/blog/master-detail-editing-inserting-deleting-with-a-listview-and-detailsview/

This will give you the basic idea; 这将给您基本概念; the sample linked to shows both, but as I said it is a simple matter to toggle them on and off on the click of a button. 链接到的示例同时显示了两者,但是正如我所说的,单击按钮即可将其打开和关闭,这很简单。

You can do the following approach: 您可以执行以下方法:

  1. Take HyperLink in a Gridview to show a NewsTitle: 以Gridview中的HyperLink来显示新闻标题:

     <asp:GridView ID="GridView1" runat="server"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:HyperLink runat="server" ID="hl" NavigateUrl='<%#"NewsDetails.aspx?Newsid="+ Eval("NewsId") %>' Text='<%# Eval("NewsTitle") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

    Bind the GridView datasource in code behind: 在后面的代码中绑定GridView数据源:

     protected void getNewsDetails() { DataTable dtNewsDetails = new DataTable(); //retrieve data from database into the DataTable GridView1.DataSource = dtNewsDetails; GridView1.DataBind(); } 
  2. Now as GridView HyperLink will navigate to the NewsDetails.aspx with a NewsId as querystring, show the details accordingly in a NewsDetails.aspx page: 现在,由于GridView HyperLink将导航到NewsNews.aspx,其中NewsId为querystring,因此在NewsDetails.aspx页面中相应地显示详细信息:

    protected void Page_Load(object sender, EventArgs e) { string Newsid = Request.QueryString["Newsid"].ToString(); 受保护的无效Page_Load(对象发送者,EventArgs e){字符串Newsid = Request.QueryString [“ Newsid”]。ToString(); //show the details from database using Newsid } //使用Newsid显示数据库中的详细信息}

You can add a textBox with visible attribute set as False and when you retrieve the NewsDetails set it to textBox and set it visibility to True; 您可以添加一个文本框,其可见属性设置为False,当您检索NewsDetails时,将其设置为textBox并将其可见性设置为True;

<asp:TextBox ID="NewsDetails" runat="server" TextMode="MultiLine" 
                        Height="30px" Width="200px" Visible="False" />

and

void GetNewsDetails()
{
NewsDetails.Text=GetDetailsQuery;
NewsDetails.Visible=True;
}

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

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