服务器端数据验证

来源:互联网 发布:mac全灭 编辑:程序博客网 时间:2024/06/12 01:48
现在有一个方法实现页面所有textbox的数据验证,
public static bool CheckAllItems(Page page,string strFrmName)
page:传过来的page
strFrmName:是页面上面的Formid
这个方法会检测画面上面所有的textbox的验证
这个方法依赖客户端验证的自定义属性(例如:datatype="string" nullable="no" chname="JobCardNo"),
然后在调用该方法完成简单的数据效验,具体需要可以自己再完善
例子:
       if(pageCommLibrary.CheckAllItems(this,"Form1")==false)
       {
              return;
       }
把CheckAllItems写在公用cs文件里,在需要的页面直接按上面调用既可,具体代码如下(有好的方法望指教):
public static bool     CheckAllItems(Page page,string strFrmName)
        
{
            
string strDataType="";
            
string strNullable="";
            
string strChname="";
            
string strCheckText="";

            Array array
= Array.CreateInstance(typeof(string), 50);

            
foreach(System.Web.UI.Control Ctr in page.Controls)
            
{
                
if(Ctr.ID==null)
                
{
                    
continue;
                }


                
string ID=Ctr.ID.ToString();

                
if(ID.ToLower()==strFrmName.ToLower())
                
{
                    
foreach(System.Web.UI.Control CtrDet in Ctr.Controls)
                    
{
                        
if(CtrDet is System.Web.UI.WebControls.TextBox)
                        
{
                            System.Web.UI.WebControls.TextBox txtbox
=(System.Web.UI.WebControls.TextBox)CtrDet;
                            strCheckText
=txtbox.Text.Trim();

                            
if(txtbox.ReadOnly==true || txtbox.Enabled==false)
                                
continue;

                            
if(txtbox.Attributes.Keys.Count<50 && txtbox.Attributes.Keys.Count>0)
                            
{

                                txtbox.Attributes.Keys.CopyTo(array,
0);

                                
for(int i=0;i<txtbox.Attributes.Keys.Count;i++)
                                
{
                                    
string strAttItem=array.GetValue(i).ToString();
                                    
if(strAttItem.ToLower().Trim()=="datatype")
                                    
{
                                        strDataType
=txtbox.Attributes[strAttItem].Trim();
                                    }

                                    
if(strAttItem.ToLower().Trim()=="nullable")
                                    
{
                                        strNullable
=txtbox.Attributes[strAttItem].Trim();
                                    }

                                    
if(strAttItem.ToLower().Trim()=="chname")
                                    
{
                                        strChname
=txtbox.Attributes[strAttItem].Trim();
                                    }

                                }


                                
if(strNullable.Trim().ToLower()=="no")
                                
{
                                    
if(strCheckText=="")
                                    
{
                                        
//alert "not allowed null"
                                        pageCommLibrary.showJSMsgInPage(page,strChname+" don't allowed null!!");
                                        
return false;
                                    }

                                }


                                
if(txtbox.MaxLength!=0)
                                
{
                                    
if(CheckStrLen(page,strChname,strCheckText,txtbox.MaxLength)==false)
                                    
{
                                        
//alert "too long"  CheckStrLen
                                        return false;
                                    }

                                }


                                
if(strNullable.Trim().ToLower()=="no")
                                
{
                                    
if(strDataType.Trim().ToLower()=="int")
                                    
{
                                        
//alert "int"
                                        if(CheckDataType(page,strChname,strCheckText,typeof(int))==false)
                                        
{
                                            
return false;
                                        }

                                    }

                                    
else if(strDataType.Trim().ToLower()=="decimal")
                                    
{
                                        
//alert "decimal"
                                        if(CheckDataType(page,strChname,strCheckText,typeof(decimal))==false)
                                        
{
                                            
return false;
                                        }


                                    }

                                    
else if(strDataType.Trim().ToLower()=="datetime")
                                    
{
                                        
//alert "decimal"
                                        if(CheckDataType(page,strChname,strCheckText,typeof(DateTime))==false)
                                        
{
                                            
return false;
                                        }

                                    }

                                }

                                
else //if(strNullable.Trim().ToLower()=="yes")
                                {
                                    
if(strCheckText!="")
                                    
{
                                        
if(strDataType.Trim().ToLower()=="int")
                                        
{
                                            
//alert "int"
                                            if(CheckDataType(page,strChname,strCheckText,typeof(int))==false)
                                            
{
                                                
return false;
                                            }

                                        }

                                        
else if(strDataType.Trim().ToLower()=="decimal")
                                        
{
                                            
//alert "decimal"
                                            if(CheckDataType(page,strChname,strCheckText,typeof(decimal))==false)
                                            
{
                                                
return false;
                                            }


                                        }

                                        
else if(strDataType.Trim().ToLower()=="datetime")
                                        
{
                                            
//alert "decimal"
                                            if(CheckDataType(page,strChname,strCheckText,typeof(DateTime))==false)
                                            
{
                                                
return false;
                                            }

                                        }

                                    }

                                }

                            }

                            
else
                            
...
                        }

                    }

                }

            }

            
return true;
        }
原创粉丝点击