zoukankan      html  css  js  c++  java
  • 通用类 ControlHelper 获取、绑定、校验控件值的操作方法类

    /// <summary>
        /// 获取、绑定、校验控件值的操作方法类
        /// </summary>
        public class ControlHelper
        {
            /// <summary>
            /// 文本框不能为空值
            /// </summary>
            /// <param name="txt">文件框控件ID</param>
            /// <param name="txtName">文本框控件名称</param>
            /// <returns></returns>
            public static string GetErrorMessage(TextBox txt, string txtName)
            {
                if(txt.Text.Trim() == string.Empty)
                    return string.Format("<li>{0}不能为空值!</li>", txtName);
                else
                    return string.Empty;
            }
    
            /// <summary>
            /// 文本框不能为空值且不能超过允许字符
            /// </summary>
            /// <param name="txt">文本框控件ID</param>
            /// <param name="txtName">文本框控件名称</param>
            /// <param name="maxLength">最大长度</param>
            /// <returns></returns>
            public static string GetErrorMessage(TextBox txt, string txtName, int maxLength)
            {
                if(txt.Text.Trim() == string.Empty || txt.Text.Trim().Length > maxLength)
                    return string.Format("<li>{0}不能为空值且不能超过{1}个字符!</li>", txtName, maxLength);
                else
                    return string.Empty;
            }
    
            /// <summary>
            /// 文本控件值校验类型
            /// </summary>
            public enum TextValueType
            {
                /// <summary>
                /// int 类型,包括负数
                /// </summary>
                Int,
                /// <summary>
                /// 小数类型
                /// </summary>
                Decimal,
                /// <summary>
                /// 整形数字类型
                /// </summary>
                Numeric
            }
    
            /// <summary>
            /// 检查文本框必须为数字
            /// </summary>
            /// <param name="txt">文本框控件ID</param>
            /// <param name="txtName">文本框控件名称</param>
            /// <param name="isInt">是否为Int数字值,否则为小数值</param>
            /// <param name="isRequire">是否必须输入</param>
            /// <returns></returns>
            public static string GetErrorMessage(TextBox txt, string txtName, TextValueType tvt, bool isRequire)
            {
                if(isRequire && txt.Text.Trim() == string.Empty)
                    return string.Format("<li>{0}不能为空值!</li>", txtName);
    
                switch(tvt)
                {
                    case TextValueType.Int:
                        if(!Validator.IsInt(txt.Text.Trim()) && txt.Text.Trim() != string.Empty)
                            return string.Format("<li>{0}的值必须为Int型数字!</li>", txtName);
                        break;
                    case TextValueType.Decimal:
                        if(!Validator.IsDecimal(txt.Text.Trim()) && txt.Text.Trim() != string.Empty)
                            return string.Format("<li>{0}的值必须为数字或小数且不能为负数!</li>", txtName);
                        break;
                    case TextValueType.Numeric:
                        if(!Validator.IsNumeric(txt.Text.Trim()) && txt.Text.Trim() != string.Empty)
                            return string.Format("<li>{0}的值必须为整型数字!</li>", txtName);
                        break;
                }
    
    
                return string.Empty;
            }
    
    
            /// <summary>
            /// 检验下拉框必须选择索引大于第一项的值
            /// </summary>
            /// <param name="ddl"></param>
            /// <param name="ddlName"></param>
            /// <returns></returns>
            public static string GetErrorMessage(DropDownList ddl, string ddlName)
            {
                if(ddl.SelectedIndex <= 0)
                    return string.Format("<li>请选择{0}!</li>", ddlName);
                else
                    return string.Empty;
            }
    
            /// <summary>
            /// 检验多项单选框必须选择
            /// </summary>
            /// <param name="ddl"></param>
            /// <param name="ddlName"></param>
            /// <returns></returns>
            public static string GetErrorMessage(RadioButtonList rbl, string rblName)
            {
                if(rbl.SelectedIndex == -1)
                    return string.Format("<li>请选择{0}!</li>", rblName);
                else
                    return string.Empty;
            }
    
    
    
            /// <summary>
            /// 只允许上传图片
            /// </summary>
            /// <param name="upload"></param>
            /// <param name="fuName"></param>
            /// <returns></returns>
            public static string GetErrorMessage(FileUpload upload, string fuName)
            {
                if(upload.HasFile)
                {
                    if(upload.PostedFile.ContentType.ToString().ToLower().IndexOf("image") == -1)
                        return string.Format("<li>上传的{0}文件格式不正确!</li>", fuName);
                }
                return string.Empty;
            }
    
    
            /// <summary>
            /// 获得文本框的数字值
            /// </summary>
            /// <param name="txt">文本框控件</param>
            /// <returns></returns>
            public static int GetIntValue(TextBox txt)
            {
                if(txt.Text.Trim() == string.Empty)
                    return 0;
                try
                {
                    return Convert.ToInt32(txt.Text.Trim());
                }
                catch
                {
                    return 0;
                }
            }
    
            /// <summary>
            /// 获得下拉控件选择项索引大于0的Int值
            /// </summary>
            /// <param name="ddl"></param>
            /// <returns></returns>
            public static int GetIntValue(DropDownList ddl)
            {
                if(ddl.SelectedIndex > 0)
                    return Convert.ToInt32(ddl.SelectedValue);
                else
                    return 0;
            }
    
    
            /// <summary>
            /// 获得文本框的小数值
            /// </summary>
            /// <param name="txt">文本框控件</param>
            /// <param name="IsDecimal">是否为小数</param>
            /// <returns></returns>
            public static decimal GetDecimalValue(TextBox txt)
            {
                if(txt.Text.Trim() == string.Empty)
                    return 0;
                try
                {
                    return Convert.ToDecimal(txt.Text.Trim());
                }
                catch
                {
                    return 0;
                }
            }
    
    
    
            /// <summary>
            /// 返回从CheckBoxList中选定的单个或多个值
            /// </summary>
            /// <param name="cbl">CheckBoxList控件名称</param>
            /// <returns>返回选定的值</returns>
            public static string GetValue(CheckBoxList cbl)
            {
                string result = string.Empty;
                for(int i = 0; i < cbl.Items.Count; i++)
                {
                    if(cbl.Items[i].Selected == true)
                        result += String.Format(",{0}", cbl.Items[i].Value);
                }
                if(result != string.Empty)
                    result = result.Substring(1, result.Length - 1);
                return result;
            }
    
    
            /// <summary>
            /// 从CheckBoxList中选定
            /// </summary>
            /// <param name="cbl">CheckBoxList控件名称</param>
            /// <param name="val">要选中项值,多个值以,号间隔</param>
            public static void BindValue(CheckBoxList cbl, string val)
            {
                Array.ForEach(val.Split(','),
                delegate(string s)
                {
                    for(int i = 0; i < cbl.Items.Count; i++)
                        if(cbl.Items[i].Value == s)
                            cbl.Items[i].Selected = true;
                });
            }
    
    
            /// <summary>
            /// 从单选项中选定
            /// </summary>
            /// <param name="rbl">RadioButtonList控件名称</param>
            /// <param name="val">要选定的值</param>
            public static void BindValue(RadioButtonList rbl, string val)
            {
                for(int i = 0; i < rbl.Items.Count; i++)
                {
                    if(rbl.Items[i].Value == val)
                        rbl.Items[i].Selected = true;
                }
            }
        }
    
  • 相关阅读:
    张拥军:解码商品期货投资实战应用 2011年06月02日
    罗小奔:谈一下最近商品期货套利的几个机会
    罗小奔:商品期货套利当前之我见
    leetcode -- Binary Tree Preorder Traversal
    leetcode -- Linked List Cycle II
    leetcode -- Linked List Cycle
    [转载]2014年10月26完美世界校招两道java题
    [转载]SQL truncate 、delete与drop区别
    [转载]会引起全表扫描的几种SQL
    [转载]Linux I/O 调度方法
  • 原文地址:https://www.cnblogs.com/acyy/p/2662161.html
Copyright © 2011-2022 走看看