在ASP.NET三层架构中,设计一个类似于淘宝的展示商品界面的网站(连接已有的SQL数据库,在网站中展示各种商品的图片加名字,一行四个商品,依次罗列下来)给出.asox的代码和.aspx.cs代码
时间: 2024-03-10 19:46:38 浏览: 21
好的,我来给你提供一下代码。
首先是 .asox 文件的代码:
```aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>商品展示</title>
<style type="text/css">
.product {
float: left;
margin: 10px;
width: 200px;
text-align: center;
}
.product img {
width: 150px;
height: 150px;
}
.product p {
margin-top: 5px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>商品展示</h1>
<div>
<asp:Repeater ID="rptProducts" runat="server">
<ItemTemplate>
<div class="product">
<img src='<%# Eval("ImageUrl") %>' />
<p><%# Eval("ProductName") %></p>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</form>
</body>
</html>
```
然后是 .aspx.cs 文件的代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindProducts();
}
}
private void BindProducts()
{
string connectionString = "your_connection_string_here";
string query = "SELECT ProductName, ImageUrl FROM Products";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
rptProducts.DataSource = reader;
rptProducts.DataBind();
reader.Close();
}
catch (Exception ex)
{
// Handle the exception
}
}
}
}
```
其中,你需要将 `your_connection_string_here` 替换为你自己的数据库连接字符串。
这样,你就可以在 ASP.NET 三层架构中设计一个类似于淘宝的展示商品界面的网站了。