几个常用的方法

    /// <summary>  
    /// 判断传入的字符串是否是为整数  
    /// </summary>  
    /// <param name="strContent">待判断的字符串</param>  
    /// <returns>判断结果</returns>  
    public static bool IsInteger(string strContent)  
    {  
        bool IsInt = false;  
        int result;  
        if (!System.String.IsNullOrEmpty(strContent))  
        {  
            IsInt = int.TryParse(strContent, out result);  
        }  
        return IsInt;  
    }  
    /// <summary>  
    /// 将传入的字符串转换为整数,如果不是则返回默认值  
    /// </summary>  
    /// <param name="strContent">待转换的字符串</param>  
    /// <param name="defaultValue">转换失败的默认值</param>  
    /// <returns>判断结果</returns>  
    public static int GetInteger(string strContent, int defaultValue)  
    {  
        int result = defaultValue;  
        if (!System.String.IsNullOrEmpty(strContent))  
        {  
            bool isInt = int.TryParse(strContent, out result);  
            if (!isInt)  
            {  
                result = defaultValue;  
            }  
        }  
        return result;  
    }  
    /// <summary>  
    /// 截取中文字符串  
    /// </summary>  
    /// <param name="inputString">原始字符串</param>  
    /// <param name="length">截取长度</param>  
    /// <returns>截取后的字符串</returns>  
    public static string SubString(string inputString, int length)  
    {  
        if (Encoding.UTF8.GetByteCount(inputString) <= length * 2)  
        {  
            return inputString;  
        }  
        ASCIIEncoding ascii = new ASCIIEncoding();  
        int tempLen = 0;  
        string tempString = "";  
        byte[] s = ascii.GetBytes(inputString);  
        for (int i = 0; i < s.Length; i++)  
        {  
            if ((int)s[i] == 63)  
            {  
                tempLen += 2;  
            }  
            else  
            {  
                tempLen += 1;  
            }  
            tempString += inputString.Substring(i, 1);  
            if (tempLen >= (length - 1) * 2)  
                break;  
        }  
        //如果截过则加上半个省略号  
        if (System.Text.Encoding.Default.GetBytes(inputString).Length > length)  
            tempString += "...";  
        return tempString;  
    }  
    /// <summary>  
    /// 截取字符串  
    /// </summary>  
    /// <param name="input">待截取的字符串</param>  
    /// <param name="length">从字符串最后面去掉的位数</param>  
    /// <returns></returns>  
    public string CutString(string input, int length)  
    {  
        string result = string.Empty;  
        if (input == null)  
            return string.Empty;  
        if (input.Length > length)  
        {  
            result = input.Substring(0, input.Length - length);  
        }  
        return result;  
    }  
        /// <summary>
        /// 循环所有输入框设为不可用,调用TextBoxDisabled(this.Page.Controls);
        /// </summary>
        /// <param name="objControlCollection"></param>
        private void TextBoxDisabled(ControlCollection objControlCollection)
        {
            foreach (System.Web.UI.Control objControl in objControlCollection)
            {
                if (objControl.HasControls())
                {
                    TextBoxDisabled(objControl.Controls);
                }
                else
                {
                    if (objControl is System.Web.UI.WebControls.TextBox)
                    {
                        ((TextBox)objControl).Enabled = false;
                    }
                }
            }
        }  


//或者调用js
 ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript1", "<script>inputsdisabled();</script>");

function inputsdisabled() {
        var controls = document.getElementsByTagName('input');
        for (var i = 0; i < controls.length; i++) {
            if (controls[i].type == 'text') {
                controls[i].disabled = true;
            }
        }
    } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值