文本框中的值在更新语句上始终相同



>我在页面加载时从数据库中选择值到文本框中。然后,当我更改它们并想要更新数据库时,值与原始值相同。例如,我在文本框名称中选择名称罗宾汉,将其更改为比尔盖茨,但更新时文本框的值仍然是罗宾汉如何修复此行为?

但是,这仅适用于带有TextMode="SingleLIne""MultiLine"的文本框。例如,当文本框具有TextMode="Url"时,它可以正常工作。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 'Bind
    Try
        Using conn As New SqlConnection(connStr)
            Dim cmd As SqlCommand = conn.CreateCommand
            cmd.CommandText = "SELECT * FROM Profiles WHERE (ProfileId = @ProfileId)"
            cmd.Parameters.AddWithValue("@ProfileId", Request.QueryString("id"))
            conn.Open()
            Dim rd As SqlDataReader = cmd.ExecuteReader()
            While rd.Read()
                ProfileImage.ImageUrl = rd.Item("ProPicUrl")
                txtName.Text = rd.Item("Name")
                txtCity.Text = rd.Item("City")
                drpRegion.Items.FindByText(rd.Item("Region")).Selected = True
                txtAge.Text = rd.Item("Age")
                RadioButtonList1.Items.FindByText(rd.Item("Sex")).Selected = True
                txtLink.Text = rd.Item("Link")
                txtPhone.Text = rd.Item("Phone")
                txtAbout.Text = rd.Item("About")
                txtMotto.Text = rd.Item("Motto")
                txtGoal.Text = rd.Item("Goal")
                txtHobby.Text = rd.Item("Hobby")
            End While
            conn.Close()
        End Using
    Catch ex As Exception
    End Try
End Sub
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim fileUrl As String = "~/ProPics/"
    Dim name As String = txtName.Text
    Try
        'Save profile picture
        Try
            If FileUpload1.HasFile Then
                fileUrl += FileUpload1.FileName
                FileUpload1.SaveAs(Server.MapPath(fileUrl))
            Else
                fileUrl = ProfileImage.ImageUrl
            End If
        Catch ex As Exception
            UploadMessage.Text = "Nastala chyba při nahrávání obrázku." + vbCrLf + "Chybové hlášení: " + ex.Message
        End Try
        Using conn As New SqlConnection(connStr)
            Dim cmd As SqlCommand = conn.CreateCommand
            cmd.CommandText = "UPDATE Profiles SET Name = @Name, ProPicUrl = @Url, City = @City, Region = @Region, Age = @Age, Sex = @Sex, Link = @Link, Phone = @Phone, About = @About, Motto = @Motto, Goal = @Goal, Hobby = @Hobby WHERE (ProfileId = @ProfileId)"
            cmd.Parameters.AddWithValue("@Url", fileUrl)
            cmd.Parameters.AddWithValue("@Name", name)
            cmd.Parameters.AddWithValue("@City", txtCity.Text)
            cmd.Parameters.AddWithValue("@Region", drpRegion.SelectedItem.Text)
            cmd.Parameters.AddWithValue("@Age", txtAge.Text)
            cmd.Parameters.AddWithValue("@Sex", RadioButtonList1.SelectedItem.Text)
            cmd.Parameters.AddWithValue("@Phone", txtPhone.Text)
            cmd.Parameters.AddWithValue("@Link", txtLink.Text)
            cmd.Parameters.AddWithValue("@About", txtAbout.Text)
            cmd.Parameters.AddWithValue("@Motto", txtMotto.Text)
            cmd.Parameters.AddWithValue("@Goal", txtGoal.Text)
            cmd.Parameters.AddWithValue("@Hobby", txtHobby.Text)
            cmd.Parameters.AddWithValue("@ProfileId", Request.QueryString("id"))
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            'Refresh page
            Response.Redirect(Request.RawUrl)
        End Using
    Catch ex As Exception
    End Try
End Sub

Page_Load 事件中执行代码时,需要添加对页面的 IsPostBack 属性的检查。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    if Not PostBack Then
       ...... code to execute only the first time the Page_Load is called
       Try

       End Try
       Catch ex As Exception
       End Try
    End If 
    .. code to execute every time....
End Sub

当用户单击带有Runat=Server的按钮时,该按钮会在服务器端代码上调用事件,但这会导致Page_Load新的调用。

实际上,每次执行 Page_Load 事件时,您的代码都会从数据库中重新加载原始值,因此您的按钮单击事件代码会看到数据库中的原始值,而不是修改后的值。

这篇关于页面生命周期的文章在这里可能很有用

最新更新