我创建了一个母版页。我创建了一个名为 default.aspx 的默认页面。默认的 aspx 文件包含所有标准内容和 ID。在 ID 中,我制作了一个 div 标签来保存动态页面的内容。
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Default.aspx.vb" Inherits="P03_S02.Link1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="DynPage" runat="server"></div>
</asp:Content>
这一切都很好。因此,通过使用 Visual Studio 2012 附带的 SQL server express,我创建了一个包含数据的表。表中的实体是 ProductID Name Price Quantity
现在在 default.aspx.vb 页面中我完成了以下代码。
Imports System.Data
Imports System.Data.SqlClient
Public Class Catelog
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim Connection As SqlConnection
Dim Command As SqlCommand
Dim Reader As SqlDataReader
Connection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Products.mdf;Integrated Security=True")
Dim CommandString As String
CommandString = "SELECT * FROM Product"
Command = New SqlCommand(CommandString)
Command.CommandType = CommandType.Text
Command.Connection = Connection
Command.Connection.Open()
Command.ExecuteNonQuery()
Reader = Command.ExecuteReader(CommandBehavior.CloseConnection)
Dim ProductList As String
If Reader.HasRows Then
ProductList &= "<table width=""100%"">"
ProductList &= " <tr bgcolor=""#00CCFF"">"
ProductList &= "<td><b>ID</b></td>"
ProductList &= "<td><b>Product</b></td>"
ProductList &= "<td><b>Price</b></td>"
ProductList &= "<td><b>Quantity</b></td>"
ProductList &= "</tr>"
While Reader.Read
Dim newProduct As String
ProductList &= "<tr>"
ProductList &= "<td>" & Reader("ProductID") & "</td>" & _
"<td>" & "<a href=""Link1.aspx?ProdID=""" & Reader("Name") & ">" & Reader("Name") & "</a>" & "</td>" & _
"<td>" & Reader("Price") & "</td>" & _
"<td>" & Reader("Quantity") & "</td>"
ProductList &= "</tr>"
End While
ProductList &= "</table>"
End If
Catelog.InnerHtml = ProductList
Command.Connection.Close()
' Command.Dispose()
Connection.Dispose()
ProductCatelog.InnerHtml = ProductList
End If
End Sub
End Class
如您所见,表格将显示表格中的所有数据。在 while 循环中,我将表中的每个名称超链接到另一个名为 Link1 的 apsx 文件。
我使用与上面相同的代码,但在 Link1.aspx.vb 中更改了一些内容
添加:
Dim ProductID As String
ProductID = Request.QueryString("ProdID").ToString()'problem possible here
更改了数据的显示:
Dim ProductList As String
If Reader.HasRows Then
ProductList &= "<table width=""100%"">"
ProductList &= " <tr bgcolor=""#00CCFF"">"
ProductList &= "<td><b>Product</b></td>"
ProductList &= "<td><b>Price</b></td>"
ProductList &= "<td><b>Quantity</b></td>"
ProductList &= "</tr>"
While Reader.Read
Dim newProduct As String
ProductList &= "<tr>"
ProductList &= "<td>" & Reader("Name") & "</td>" & _
"<td>" & Reader("Price") & "</td>" & _
"<td>" & Reader("Quantity") & "</td>"
ProductList &= "</tr>"
End While
ProductList &= "</table>"
End If
使用以下内容仅显示一条记录:
Dim CommandString As String
CommandString = "SELECT * FROM Product where ProductID =" & ProductID
我的目标是,如果您单击一个名称,它将链接到 Link1.aspx 并仅显示有关该名称的信息(表中的信息)。这不会发生,因为程序崩溃了。我已经使用了我所有的基本调试知识。