basketform.aspx.vb
来自「Mastering VBNet Include Source Code」· VB 代码 · 共 87 行
VB
87 行
Public Class BasketForm
Inherits System.Web.UI.Page
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents lblBasket As System.Web.UI.WebControls.Label
Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Protected WithEvents DASelectedProducts As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
Me.DASelectedProducts = New System.Data.SqlClient.SqlDataAdapter()
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "data source=(local);initial catalog=Northwind;persist security info=False;user id" & _
"=sa;workstation id=PROTSERVER;packet size=4096"
'
'DASelectedProducts
'
Me.DASelectedProducts.SelectCommand = Me.SqlSelectCommand1
Me.DASelectedProducts.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Products", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ProductID", "ProductID"), New System.Data.Common.DataColumnMapping("UnitPrice", "UnitPrice")})})
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim total As Decimal
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim ck As Object
Dim cmd As String
cmd = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductID IN ("
For Each ck In Session
cmd = cmd & ck.ToString & ", "
Next
cmd = cmd.Substring(0, cmd.Length - 2)
cmd = cmd & ")"
SqlConnection1.Open()
DASelectedProducts.SelectCommand.CommandText = cmd
DASelectedProducts.SelectCommand.CommandType = CommandType.Text
Dim Reader As Data.SqlClient.SqlDataReader = DASelectedProducts.SelectCommand.ExecuteReader
Dim prodName As String, prodID As Integer, prodPrice As Decimal
Dim subTotal As Decimal
Dim table As String
table = "<table border>"
table = table & "<tr>"
table = table & "<td><b>Product Name</b></td><td><b>Quantity</b></td>"
table = table & "<td><B>Price</b></td><td><b>Subtotal</b></td></tr>"
While Reader.Read
table = table & "<tr><td>"
prodName = Reader.Item("ProductName")
prodID = Reader.Item("ProductID")
prodPrice = Reader.Item("UnitPrice")
table = table & prodName
table = table & "</td><td align=right>" & Session(CStr(prodID))
table = table & "<td align=right>" & prodPrice & "</td>"
subTotal = prodPrice * Session(CStr(prodID))
table = table & "<td align=right>" & FormatCurrency(subTotal, 2)
total = total + subTotal
End While
table = table & "</tr></tsble>"
table = table & "<p><b>Your total is " & total.ToString & "</b></p>"
lblBasket.Text = table
SqlConnection1.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("BuyForm.aspx?Total=" & Server.UrlEncode(total))
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?