マスターページを作成しました。そして、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
以下を使用して、1 つのレコードのみを表示しました。
Dim CommandString As String
CommandString = "SELECT * FROM Product where ProductID =" & ProductID
私の目標は、名前をクリックすると Link1.aspx にリンクされ、その名前に関する情報のみが表示されることです (表の情報)。プログラムがクラッシュするため、それは起こりません。基本的なデバッグの知識はすべて使用しました。