FormView控件的三种数据验证方法

FormView控件的三种数据验证方法

利用GridView和FormView可以很方便地更新、插入记录到数据库中,但在对数据库进行操作时对数据的验证非常重要。

本文简单介绍了FormView控件插入记录时的三种数据验证方法。

(一) 利用验证服务器控件
进入Formview的模版编辑状态,选择InsertItemTemplate模版或EditItemTemplate模版,
将验证服务器控件(如RequiredFieldValidator)拖放到打开的相应模版,然后在CtrolToValidate属性中指定需要验证的相应字段,在ErrorMessage属性中输入出错提示信息即可。

(二) 利用FormView的ItemInserting和ItemUpdating事件
例如:
    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
        Dim sScript As String
        If String.IsNullOrEmpty(e.Values.Item("book_id")) Then
            sScript = "alert('book_id为空!!')"
            Me.ClientScript.RegisterStartupScript(Me.GetType, "error", sScript, True)
            e.Cancel = True
        End If
    End Sub

(三) 利用与FormView绑定的SqlDataSource数据源控件的Inserting和Updating事件
例如:
    Protected Sub SqlDataSource2_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource2.Inserting
        Dim sScript As String
        If String.IsNullOrEmpty(e.Command.Parameters("@book_id").Value) Then
            sScript = "alert('book_id为空!')"
            Me.ClientScript.RegisterStartupScript(Me.GetType, "error", sScript, True)
            e.Cancel = True
        End If
    End Sub

这三种方法中,本人认为利用SqlDataSource.Inserting方法(或SqlDataSource.Updating方法)最可取。第一种方法很方便,但想做到出错信息集中处理就较难。第三种方法移植性较好,GridView控件、DetailsView控件均可使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值