当我试图在用户提交后将我的页面加载值放入数据库时,我遇到了一个奇怪的问题。
当页面加载我有这个:
Public theProductName As String = ""
Dim theProductCategory As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadProductName()
End If
End Sub
Private Sub LoadProductName()
objConn = New MySqlConnection(product.strConnString)
objConn.Open()
strSQL = "SELECT id, name, category " & _
"FROM product " & _
"WHERE id = 4;"
Try
objCmd = New MySqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
While dtReader.Read()
theProductName = dtReader("name")
theProductCategory = dtReader("category")
btnSubmit.Text = "Add Item to " & theProductName
End While
dtReader.Close()
dtReader = Nothing
objConn.Close()
objConn = Nothing
Catch ex As Exception
MsgBox("LoadProductName: " & ex.Message)
End Try
End Sub
当我在页面启动时检查这些值时,它们具有正确的值,甚至提交按钮也具有产品名称。
但是,一旦我在按下按钮后保存它,它就具有与没有相同的变量?
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim productName As String = theProductName.Replace(" ", "-")
Dim imageName As String = productName & "\" & Trim(txtModel.Text) & ".jpg'"
Dim theCat as String = theProductCategory
End Sub
这里的 productName 和 theCat 一样什么都不是
我可以忽略什么?