Code:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports AspDotNetStorefrontCore
Imports System.Security.Permissions
Namespace AspDotNetStorefront
Public Class QuickOrderFormRow
Inherits TableRow
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Private NotInheritable Class CustomSKUBox
Inherits System.Web.UI.WebControls.TextBox
Public isDirty As Boolean = False
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(e)
isDirty = True
RaiseBubbleEvent(Me, e)
End Sub
End Class
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Private NotInheritable Class CustomQtyBox
Inherits System.Web.UI.WebControls.TextBox
Public isDirty As Boolean = False
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(e)
Me.Text = Regex.Replace(Me.Text, "[^0-9]", "")
isDirty = True
RaiseBubbleEvent(Me, e)
End Sub
End Class
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Private NotInheritable Class CustomDropDownList
Inherits System.Web.UI.WebControls.DropDownList
Public isDirty As Boolean = False
Protected Overrides Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)
MyBase.OnSelectedIndexChanged(e)
isDirty = True
RaiseBubbleEvent(Me, e)
End Sub
End Class
Private tcTxtSKU As TableCell
Private tcLstSKU As TableCell
Private tcTxtQty As TableCell
Private tcLbl As TableCell
Private upTxtSKU As UpdatePanel
Private upLstSKU As UpdatePanel
Private upTxtQty As UpdatePanel
Private upLbl As UpdatePanel
Private upLabels As UpdatePanel
Private txtSKU As CustomSKUBox
Private lstSKUSuffix As CustomDropDownList
Private txtQty As CustomQtyBox
Private lblStatus As Label
Private lblProductID As Label
Private lblVariantID As Label
Private RowID As String
Private _CurrentID As Integer = 0
'make sure that the IDs are assigned the same every time for ViewState consistancy
'the RowID is needed to make sure that IDs are different between rows
Private ReadOnly Property NextID() As Integer
Get
_CurrentID += 1
Return RowID & _CurrentID
End Get
End Property
Public ReadOnly Property ReadyToAddToCart() As Boolean
Get
Return txtQty.Enabled AndAlso lblProductID.Text <> "" AndAlso lblVariantID.Text <> "" AndAlso txtQty.Text <> ""
End Get
End Property
Public ReadOnly Property Quantity() As Integer
Get
Return txtQty.Text
End Get
End Property
Public ReadOnly Property ProductID() As String
Get
Return lblProductID.Text
End Get
End Property
Public ReadOnly Property VariantID() As String
Get
Return lblVariantID.Text
End Get
End Property
Public ReadOnly Property isDirty() As Boolean
Get
Return txtSKU.isDirty Or txtQty.isDirty Or lstSKUSuffix.isDirty
End Get
End Property
Public Sub New(ByVal clientID As String, ByVal myRowID As String, ByVal myScriptManager As ScriptManager)
MyBase.New()
'this will save the controls ID to a hidden field for recall later to reset the focus
Dim strJava As String = "document.getElementById('" & clientID & "').value = this.id;"
RowID = myRowID
tcTxtSKU = New TableCell
tcLstSKU = New TableCell
tcTxtQty = New TableCell
tcLbl = New TableCell
upTxtSKU = New UpdatePanel
upTxtSKU.UpdateMode = UpdatePanelUpdateMode.Conditional
upLstSKU = New UpdatePanel
upLstSKU.UpdateMode = UpdatePanelUpdateMode.Conditional
upTxtQty = New UpdatePanel
upTxtQty.UpdateMode = UpdatePanelUpdateMode.Conditional
upLbl = New UpdatePanel
upLbl.UpdateMode = UpdatePanelUpdateMode.Conditional
txtSKU = New CustomSKUBox
txtSKU.AutoPostBack = True
txtSKU.ID = NextID
txtSKU.Attributes("onfocus") = strJava
upTxtSKU.ContentTemplateContainer.Controls.Add(txtSKU)
tcTxtSKU.Controls.Add(upTxtSKU)
lstSKUSuffix = New CustomDropDownList
lstSKUSuffix.Visible = False
lstSKUSuffix.AutoPostBack = True
lstSKUSuffix.ID = NextID
lstSKUSuffix.Attributes("onfocus") = strJava
upLstSKU.ContentTemplateContainer.Controls.Add(lstSKUSuffix)
tcLstSKU.Controls.Add(upLstSKU)
txtQty = New CustomQtyBox
txtQty.Columns = 4
txtQty.AutoPostBack = True
txtQty.ID = NextID
txtQty.Attributes("onfocus") = strJava
upTxtQty.ContentTemplateContainer.Controls.Add(txtQty)
tcTxtQty.Controls.Add(upTxtQty)
lblStatus = New Label
upLbl.ContentTemplateContainer.Controls.Add(lblStatus)
lblProductID = New Label
lblProductID.Visible = False
upLbl.ContentTemplateContainer.Controls.Add(lblProductID)
lblVariantID = New Label
lblVariantID.Visible = False
upLbl.ContentTemplateContainer.Controls.Add(lblVariantID)
tcLbl.Controls.Add(upLbl)
Me.Controls.Add(tcTxtSKU)
Me.Controls.Add(tcLstSKU)
Me.Controls.Add(tcTxtQty)
Me.Controls.Add(tcLbl)
'needed to register the controls for async operations to prevent page refreshes
myScriptManager.RegisterAsyncPostBackControl(txtSKU)
myScriptManager.RegisterAsyncPostBackControl(lstSKUSuffix)
myScriptManager.RegisterAsyncPostBackControl(txtQty)
End Sub
Protected Overrides Function OnBubbleEvent(ByVal source As Object, ByVal args As System.EventArgs) As Boolean
'all the changable controls will force a Bubble event that will be handled here by updating the fields
If isDirty Then
GetNameAndIDs()
End If
If txtSKU.isDirty Then
upTxtSKU.Update()
End If
If lstSKUSuffix.isDirty Then
upLstSKU.Update()
End If
If txtQty.isDirty Then
upTxtQty.Update()
End If
Return MyBase.OnBubbleEvent(source, args)
End Function
Public Sub DisableAll()
txtSKU.Enabled = False
lstSKUSuffix.Enabled = False
txtQty.Enabled = False
End Sub
Public Sub Clear()
lstSKUSuffix.Items.Clear()
txtQty.Text = ""
lblStatus.Text = ""
lblProductID.Text = ""
lblVariantID.Text = ""
End Sub
Public Sub GetNameAndIDs()
If txtSKU.isDirty Then
lstSKUSuffix.Items.Clear()
lstSKUSuffix.Items.Add("--Choose One--")
Using dbconn As New SqlConnection(DB.GetDBConn())
dbconn.Open()
Using rs As IDataReader = DB.GetRS("Select ProductVariant.SKUSuffix FROM Product INNER JOIN ProductVariant ON Product.ProductID = ProductVariant.ProductID WHERE (Product.SKU = N'" & txtSKU.Text & "')", dbconn)
While rs.Read()
lstSKUSuffix.Items.Add(DB.RSField(rs, "SKUSuffix"))
End While
End Using
End Using
If lstSKUSuffix.Items.Count > 2 Then
lstSKUSuffix.Visible = True
Else
lstSKUSuffix.Visible = False
End If
upLstSKU.Update()
End If
'clear old data
lblStatus.Text = ""
lblProductID.Text = ""
lblVariantID.Text = ""
If lstSKUSuffix.Visible Then 'if there are SKUSuffixes
If lstSKUSuffix.SelectedIndex = 0 Then 'if a SKUSuffix hasn't been choosen
'get just the name
Using dbconn As New SqlConnection(DB.GetDBConn)
dbconn.Open()
Using rs As IDataReader = DB.GetRS("SELECT Product.Name FROM Product INNER JOIN ProductVariant ON Product.ProductID = ProductVariant.ProductID WHERE (ProductVariant.IsDefault = 1) AND (Product.SKU = N'" & txtSKU.Text & "')", dbconn)
While rs.Read
lblStatus.Text = DB.RSField(rs, "Name") & " - Please Choose A Variant."
Exit While 'only get the first one, should only be one
End While
End Using
End Using
Else 'if a SKUSuffix has been choose, get the info
Using dbconn As New SqlConnection(DB.GetDBConn)
dbconn.Open()
Using rs As IDataReader = DB.GetRS("SELECT Product.Name + '; ' + ProductVariant.Name AS Name, Product.ProductID, ProductVariant.VariantID FROM Product INNER JOIN ProductVariant ON Product.ProductID = ProductVariant.ProductID WHERE (Product.SKU = N'" & txtSKU.Text & "') AND (ProductVariant.SKUSuffix = N'" & lstSKUSuffix.Text & "')", dbconn)
While rs.Read()
lblStatus.Text = DB.RSField(rs, "Name")
lblProductID.Text = DB.RSFieldInt(rs, "ProductID")
lblVariantID.Text = DB.RSFieldInt(rs, "VariantID")
Exit While 'only get the first one, should only be one
End While
End Using
End Using
End If
Else 'if there aren't SKUSuffixes, try to get the info on what we have
If txtSKU.Text <> "" Then
lblStatus.Text = "Product Not Found"
Using dbconn As New SqlConnection(DB.GetDBConn)
dbconn.Open()
Using rs As IDataReader = DB.GetRS("SELECT Product.ProductID, ProductVariant.VariantID, Product.Name FROM Product INNER JOIN ProductVariant ON Product.ProductID = ProductVariant.ProductID WHERE (ProductVariant.IsDefault = 1) AND (Product.SKU = N'" & txtSKU.Text & "')", dbconn)
While rs.Read
lblStatus.Text = DB.RSField(rs, "Name")
lblProductID.Text = DB.RSFieldInt(rs, "ProductID")
lblVariantID.Text = DB.RSFieldInt(rs, "VariantID")
Exit While 'only get the first one, should only be one
End While
End Using
End Using
Else
Clear()
End If
End If
'if we found a product, but don't have a quantity, request quantity
If lblProductID.Text <> "" AndAlso lblVariantID.Text <> "" AndAlso txtQty.Text = "" Then
lblStatus.Text &= " - Please enter a quantity."
End If
upLbl.Update()
End Sub
Public Sub AddToCart(ByVal myCart As ShoppingCart, ByVal myCustomer As Customer)
Dim CartType As CartTypeEnum = CartTypeEnum.ShoppingCart
Dim ChosenColor As String = String.Empty
Dim ChosenSize As String = String.Empty
Dim ChosenColorSKUModifier As String = String.Empty
Dim ChosenSizeSKUModifier As String = String.Empty
Dim TextOption As String = String.Empty
Dim CustomerEnteredPrice As Decimal = Decimal.Zero
If ReadyToAddToCart Then
myCart.AddItem(myCustomer, 0, ProductID, VariantID, Quantity, ChosenColor, ChosenColorSKUModifier, ChosenSize, ChosenSizeSKUModifier, TextOption, CartType, False, False, 0, CustomerEnteredPrice)
lblStatus.Text &= " - Added To Cart"
DisableAll()
Else
GetNameAndIDs()
End If
End Sub
End Class
Partial Public Class quickorderform
Inherits SkinBase
Private _CurrentID As Integer = 0
'make sure that the IDs are assigned the same every time for ViewState consistancy
Private ReadOnly Property NextID() As Integer
Get
_CurrentID += 1
Return _CurrentID
End Get
End Property
Private ReadOnly Property scriptMgr() As ScriptManager
Get
Return DirectCast(Master.FindControl("scrptMgr"), ScriptManager)
End Get
End Property
Public Overrides ReadOnly Property RequireScriptManager() As Boolean
Get
Return True
End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If AppLogic.AppConfigBool("GoNonSecureAgain") Then
SkinBase.GoNonSecureAgain()
End If
Try
'set the focus to the control that had it last
scriptMgr.SetFocus(inhControlWithFocus.Value)
Catch ex As Exception
End Try
End Sub
Protected Sub quickorderform_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'check to see if the user is logged on or not
If Me.ThisCustomer.IsRegistered Then
DirectCast(Master.FindControl("PageContent"), ContentPlaceHolder).Controls.Remove(topicWelcome)
Else
topicWelcome.Visible = True
pnlUpdatePanel.Visible = False
End If
SectionTitle = "Quick Order Form"
End Sub
Protected Sub btnAddToCart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddToCart.Click
'some javascript on the onclick event will change the text and the ViewState will change it back when the page refreshes
Dim CartType As CartTypeEnum = CartTypeEnum.ShoppingCart
Me.ThisCustomer.RequireCustomerRecord()
Dim cart As New ShoppingCart(1, Me.ThisCustomer, CartType, 0, False)
'look at all the controls in the table
'if it is a quickorderformrow (a custom table row) then try to add it
For Each ctrlRow As Control In QuickOrderTable.Controls
Dim myOrderRow As QuickOrderFormRow = TryCast(ctrlRow, QuickOrderFormRow)
If myOrderRow Is Nothing Then
Continue For
End If
myOrderRow.AddToCart(cart, ThisCustomer)
Next
'update the label and then for the updatepanel to update
'this requires a modificaiton of the masterpage
DirectCast(Master.FindControl("lrtNum_Cart_Items"), Literal).Text = ShoppingCart.NumItems(ThisCustomer.CustomerID, CartTypeEnum.ShoppingCart).ToString()
DirectCast(Master.FindControl("CartUpdate"), UpdatePanel).Update()
End Sub
Private Sub AddRows()
AddRows(intNumOfRows.Value)
End Sub
Private Sub AddRows(ByVal intRows As Integer)
For x As Integer = 1 To intRows
AddRow()
Next
End Sub
Private Sub AddRow()
Dim myOrderRow As New QuickOrderFormRow(inhControlWithFocus.ClientID, NextID, scriptMgr)
QuickOrderTable.Rows.Add(myOrderRow)
End Sub
Protected Sub btnVerifyAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVerifyAll.Click
'some javascript on the onclick event will change the text and the ViewState will change it back when the page refreshes
For Each ctrlRow As Control In QuickOrderTable.Controls
Dim myOrderRow As QuickOrderFormRow = TryCast(ctrlRow, QuickOrderFormRow)
If myOrderRow Is Nothing Then
Continue For
End If
myOrderRow.GetNameAndIDs()
Next
End Sub
Protected Sub btnAddMoreRows_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMoreRows.Click
intNumOfRows.Value += 4
AddRows(4)
End Sub
Protected Sub quickorderform_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
'add rows before the StateView information is added back to the page
AddRows()
End Sub
End Class
End Namespace