Textbox.text不会在代码中显示更新的文本

时间:2013-07-25 15:51:37

标签: asp.net sql vb.net

当我点击更新时出于某种原因使用下面的代码时,hospitaltextbox.text不显示文本框中的当前值?只是最初选择的值。知道为什么它不读取文本框中当前的内容吗?

Dim reader As SqlDataReader = cmd.ExecuteReader()
        If (reader.Read()) Then

            HospitalTextBox.Text = reader(7)
            FirstNameTextBox.Text = reader(9)
            Session("ID") = reader(0)
        End If

Protected Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
    Dim Test As String
    Test = FirstNameTextBox.Text
    Try
        Dim Con As SqlConnection
        Dim cmd As SqlCommand
        Con = New SqlConnection
        Con.ConnectionString = ""
        Con.Open()
        cmd = New SqlCommand
        cmd.Connection = Con
        cmd.CommandText = "UPDATE tbltest SET [Teaching Hospital Name] = @TeachingHospitalName, [Physician First Name] = @FirstName WHERE ID = @ID"
        cmd.Parameters.Add(New SqlParameter("@ID", (Session("ID"))))
        cmd.Parameters.Add(New SqlParameter("@TeachingHospitalName", HospitalTextBox.Text)) 'does not show text that was changed in the textbox?
        cmd.Parameters.Add(New SqlParameter("@FirstName", Test))
        cmd.ExecuteNonQuery()
        Con.Close()


    Catch ex As Exception

    End Try
End Sub

1 个答案:

答案 0 :(得分:3)

检查您的Page_Load功能。

您是否正在PostBack上执行某项操作以将文本框值设置为其他内容?

如果您要绑定数据,请确保在Not IsPostback

时完成
If Not IsPostBack
   BindData()