1

基本上,在单击提交按钮进入用户表后,我将用户输入的有关其个人详细信息的数据输入到文本框中。这工作正常,直到我在这些文本框上使用验证,现在所有验证工作,但是当单击提交时,它转到我链接到按钮的页面,但是现在没有数据被输入到数据库中的用户表中。

有任何想法吗?

我的代码如下所示。谢谢

Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click

   'add new user'
    Dim db As New LostPropertyDataContext
    Dim U As New tblUser With {.Forename = txtforename.Text, 
           .Surname = txtsurname.Text, .DateOfBirth = txtdob.Text, .Gender = txtgender.Text,
           .Address = txtaddress.Text, .Postcode = txtpostcode.Text, .TelephoneNo = txttelephoneno.Text,
           .MobileNo = txtmobileno.Text, .Email = txtemail.Text}
    db.tblUsers.InsertOnSubmit(U)
    db.SubmitChanges()
    Response.Redirect("/u1065977/LostPropertyProject/LostProperty2.aspx")


End Sub
4

1 回答 1

0

当您说您添加了验证时,我假设您的意思是您向页面添加了验证控件。

这是一种很好的做法——有人可能会说,一种必要的做法——将代码包装在测试中以验证​​是否通过:

If Page.IsValid Then
   'add new user'
   Dim db As New LostPropertyDataContext
   Dim U As New tblUser With {.Forename = txtforename.Text, 
       .Surname = txtsurname.Text, .DateOfBirth = txtdob.Text, .Gender = txtgender.Text,
       .Address = txtaddress.Text, .Postcode = txtpostcode.Text, .TelephoneNo = txttelephoneno.Text,
       .MobileNo = txtmobileno.Text, .Email = txtemail.Text}
   db.tblUsers.InsertOnSubmit(U)
   db.SubmitChanges()
   Response.Redirect("/u1065977/LostPropertyProject/LostProperty2.aspx")
End If

您正在尝试更新而不检查服务器端是否已通过验证。如果您使用任何自定义验证(例如CustomValidator),您就没有机会向您报告失败的事情。

于 2013-03-12T11:12:33.627 回答