asp.net变量传递
当我试图在用户提交后将我的页面加载值放入数据库时,我有一种奇怪的感觉 当页面加载时,我有以下内容:asp.net变量传递,asp.net,variables,Asp.net,Variables,当我试图在用户提交后将我的页面加载值放入数据库时,我有一种奇怪的感觉 当页面加载时,我有以下内容: 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
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
当我在页面启动时检查这些值时,它们具有正确的值,甚至提交按钮也具有产品名称
然而,一旦我按下按钮后保存它,它就有了与nothing相同的变量
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这里什么都没有
我可能忽略了什么?这是因为只有在第一次加载时没有回发,然后没有保存在任何位置时,您才阅读产品名 您可以将变量
theProductName
置于视图状态,以便在发回后仍能使用
msdn中的示例和更多注释:您必须使用
ViewState
保存数据。因为您发布了数据,所以丢失了旧状态
public string ProductName
{
get
{
if( Viewstate["ProductName"] == null)
{
return string.Empty;
}
return (string)Viewstate["ProductName"];
}
set
{
Viewstate["ProductName"] = value;
}
}
设置值
ProductName = "your value";
什么是“产品名称”?它看起来像一个字符串-它在哪里声明/设置?@n8wrl:我已经用这些信息更新了我的OP。由于我在html页面中使用了ProductName,因此我已将其设置为Public()。如何在上面的viewstate代码中设置ProductName的值?仅使用以下代码:ProductName=“test”;我添加了布景部分谢谢你的帮助,阿吉拉斯!我很乐意帮你偷东西