我在页面加载时选择数据库中的值到文本框中。然后,当我更改它们并想要更新数据库时,值与原始值相同。例如,我将名称 Robin Hood 选择为TextBoxName,将其更改为 Bill Gates ,但更新时文本框的值仍为 Robin Hood 。我该如何解决这个问题?
但是,这仅适用于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
答案 0 :(得分:1)
在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事件时,您的代码都会从数据库重新加载原始值,因此您的按钮单击事件代码会从数据库中看到原始值而不是修改后的值。
Page Life Cycle上的这篇文章在这里很有用