在ASP.NET网页中显示动态内容(来自数据库)的最佳方式

时间:2012-12-09 06:42:19

标签: asp.net html c#-4.0

我想显示我的数据库表的一行内容,如下面的框架。表中有照片和说明列。我还想要两个按钮,如图中所提到的一些动作。我的数据库表有大约100多条记录。所以在我的网页中我想在运行时显示100帧如下,因为将来这条记录可能会达到1000.

enter image description here

我是Web应用程序开发的新手。任何人都建议我最好的GUI控制,以实现我的ASP.NET目标。任何教程或示例链接都将是很有帮助的。

感谢。

2 个答案:

答案 0 :(得分:1)

首先,我在此处创建一个具有该设计的用户控件。

然后在转发器中使用此用户控件,并将数据库参数传递给用户控件以进行正确的渲染。另外,您可以使用和DataView相同的方式。

工作示例

自定义控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ShowData.ascx.cs" Inherits="Dokimes_StackOverFlow_ShowData" %>

<asp:Literal runat="server" ID="txtTheID" EnableViewState="false" />

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

<hr /><br />

和背后的代码:

public partial class Dokimes_StackOverFlow_ShowData : System.Web.UI.UserControl
{
    public int cValueID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        txtTheID.Text = cValueID.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

以及使用它的主页:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <uc1:ShowData ID="ShowData1" runat="server" cValueID="<%# GetID(Container.DataItem) %>" />          
            </ItemTemplate>
        </asp:Repeater>    
    </div>    
    </form>
</body>
</html>

和背后的代码

public partial class Dokimes_StackOverFlow_ShowRepeatedData : System.Web.UI.Page
{
    List<int> oMainIds = new List<int>();

    override protected void OnInit(EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            oMainIds.Add(i);
        }

        Repeater1.DataSource = oMainIds;
        Repeater1.DataBind();

        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public int GetID(object oItem)
    {
        return (int)oItem;
    }
}

答案 1 :(得分:1)

您可以使用listview控件,因为它支持自定义格式。转发器是第二种选择,但listview具有比转发器更多的功能。