日期:2014-05-20  浏览次数:20433 次

aspx网页上,使用textbox等控件后,点击保存按钮如何验证其完整性?
Protected   Sub   Button1_Click(ByVal   sender   As   Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click
                Dim   cmd   As   New   SqlCommand
                Dim   dr   As   SqlDataReader
                Dim   sql   As   String

                cmd.Connection   =   cn

                If   Trim(txt_cid.Text)   =   " "   Then
                        Page.RegisterStartupScript( " ",   " <script> alert( '请输入企业编号! ') </script> ")
                        Me.txt_cid.Focus()
                        Exit   Sub
                End   If

                If   Trim(txt_name.Text)   =   " "   Then
                        Page.RegisterStartupScript( " ",   " <script> alert( '请输入企业名称! ') </script> ")
                        Me.txt_name.Focus()
                        Exit   Sub
                End   If


                Page.RegisterStartupScript( " ",   " <script> alert( '企业资料已经编辑! ') </script> ")
                Response.Redirect( "default.aspx ")
        End   Sub


当我把断点放在“   If   Trim(txt_cid.Text)   =   " "   Then”这一行的时候,假设我之前有“Me.txt_cid.Text   =   rs1.Tables( "comp ").Rows(0)( "cid ").ToString”这样的语句,并且Me.txt_cid.Text   的内容是   “abc”,然后我把Me.txt_cid.Text   的内容清空,但是当我点击Button1执行到断点的时候,为什么Me.txt_cid.Text   的值还是“abc”呢??照道理应该是空的。


另外,大家写asp.net代码的时候,一般是用什么做验证的?用前台的脚本应该怎么做?

------解决方案--------------------
像这样的可以用.NET自带的验证控件做
或者直接写脚本:
function check()
{
if(document.all.txt_cid.value == " ")
{
alert( "请输入...... ");
return false;
}
}
------解决方案--------------------
另外,大家写asp.net代码的时候,一般是用什么做验证的?用前台的脚本应该怎么做?

-------------
可以在TextBox失去焦点事件onblur来判断

TextBox1.Attributes.Add( "onblur ", "if(this.value== ' '){alert( '...为空 ');} ");

------解决方案--------------------
可能是Me.txt_cid.Text = rs1.Tables( "comp ").Rows(0)( "cid ").ToString放在了page_load里面,点击button1页面回传,又重新对Me.txt_cid.Text 赋了一次值
------解决方案--------------------
然后我把Me.txt_cid.Text 的内容清空
---------------------
检查一下这。
实在不行就在
Button1.Click
   Dim cmd As New SqlCommand