在ASP上显示数据.NET web页面

本文关键字:NET web 页面 数据 显示 ASP | 更新日期: 2023-09-27 18:17:03

大家好,我已经解决了我用中继器显示数据的问题,我已经添加了一个分页系统。现在,我一直在寻找一种方法来创建一个购物车系统使用的中继器,我已经创建了一个使用添加到购物车按钮。有没有人能提供一些提示和技巧,甚至是如何实现这一点的示例代码,下面是我的代码。

public partial class A4_Label : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeater();
        }
    }
    public void BindRepeater()
    {
        SqlConnection Connstr = new SqlConnection(ConfigurationManager.ConnectionStrings["VictoriaWorksConnectionString"].ConnectionString.ToString());
        SqlCommand cmd = new SqlCommand("SELECT [dbo].[A4 Label].ProductName,[dbo].[A4 Label].ProductType, [dbo].[A4 Label].ProductDescription, [dbo].[A4 Label Info].Quantity, [dbo].[A4 Label Info].Price FROM [dbo].[A4 Label] ,[dbo].[A4 Label Info]  WHERE  [dbo].[A4 Label].ProductID = [dbo].[A4 Label Info].ProductID ",Connstr);
        if (Connstr.State == ConnectionState.Closed)
        {
            Connstr.Open();
        }
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 15;
        pgitems.CurrentPageIndex = PageNumber;
        if (pgitems.PageCount > 1)
        {
            rptPaging.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPaging.DataSource = pages;
            rptPaging.DataBind();
        }
        else
        {
            rptPaging.Visible = false;
        }
            A4Repeater.DataSource = pgitems;
            A4Repeater.DataBind();   
    }

    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
     }

    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        BindRepeater();
    }

在ASP上显示数据.NET web页面

在ASP.NET中没有Console.WriteLine这样的东西。ASP。. NET是为Web构建的技术框架,而不是为Windows中的控制台应用程序构建的。

你想用的是这个

Response.Write(String.Format("{0}, {1}, {2}, {3}, {4} ", 
                             column[0], column[1],  column[2],  column[3],  column[4]));

这将把内容作为发送到服务器的请求的响应。我一定会把你转发给ASP。. NET论坛,在那里你可以找到关于ASP. NET的文档。净了。您可以在ASP上编写完全相同的结果。NET网页使用了许多方法,其中包括这个方法(Response.Write)和另一个方法来调用View并生成结果等等。这取决于你如何运行它

http://msdn.microsoft.com/en-us/library/ms525585 (v =应用程序). aspx

为什么不用Response.Write()

从技术上讲,这个方法在ASP中是可用的。. NET写入数据。但这不是一个好方法,这个例子就像JavaScript中的document.write(),它添加内容并覆盖其他数据。在ASP。它不做同样的事情,但它将数据添加到页面的顶部,所以像Console.WriteLine附加一样,这个方法将数据添加到body元素。

相反,您可以从数据库调用结果。之后可以在HTML正文中写入数据使用Razor语法,如

<div>@A4_Data.Readrowdata</div>

它将为您提供相同的结果,并且将位于您想要的div内。

为什么不将结果绑定到Asp GridView,让技术为您完成工作呢?

http://www.java2s.com/Code/ASP/ADO.net-Database/BindSqlDataReadertoGridView.htm

conn.Open();
command.Connection = conn;
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
GridView1.DataSource = reader;
GridView1.DataBind();