设计师自我修养左佐pdf:Asp.net常用的操作函数-程序开发-红黑联盟

来源:百度文库 编辑:九乡新闻网 时间:2024/09/21 11:17:28
Asp.net常用的操作函数
 
 
文章录入:7747.Net    责任编辑:7747.Net  18 
 【字体:小 大】
 
///
        /// 取得本周第一天的日期,即星期日
        ///

        ///
        public static string GetWeekFirstDate()
        {
            return DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();        }        ///
        /// 取得本周最后一天的日期
        ///

        ///
        public static string GetWeekLastDate()
        {
            return DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
        }        ///
        /// 转换成Long型,错误返回-1
        ///

        ///
        ///
        public static long ConvertToLong(object value)
        {
            long id = -1;
            long.TryParse(value.ToString(), out id);
            return id;
        }
        ///
        /// 转换成Double型,错误返回-1
        ///

        ///
        ///
        public static double ConvertToDouble(object value)
        {
            double id = -1;
            double.TryParse(value.ToString(), out id);
            return id;
        }        ///
        /// 转换成Int型,-1转换错误
        ///

        ///
        ///
        public static int ConvertToInt(object value)
        {
            int id = -1;
            int.TryParse(value.ToString(), out id);
            return id;
        }
        ///
        /// 绑定数据到ListControl
        ///

        ///
        ///
        ///
        ///
        public static void BindDrop(ListControl lc, DataTable dt, string strText, string strValue)
        {
            lc.Items.Clear();            foreach (DataRow myRow in dt.Rows)
            {
                lc.Items.Add(new ListItem(myRow[strText].ToString(), myRow[strValue].ToString()));
            }
            lc.DataBind();
        }        ///
        /// 绑定数据到ListControl
        ///

        ///
        ///
        ///
        ///
        public static void BindDrop(ListControl lc, DataTable dt, string strText, string strValue, string FirstText, string FirstValue)
        {
            lc.Items.Clear();
            lc.Items.Add(new ListItem(FirstText, FirstValue));
            foreach (DataRow myRow in dt.Rows)
            {
                lc.Items.Add(new ListItem(myRow[strText].ToString(), myRow[strValue].ToString()));
            }
            lc.DataBind();
        }        ///
        /// 绑定数据到ListControl
        ///

        ///
        ///
        ///
        public static void BindDrop(ListControl lc, string FirstText, string FirstValue)
        {
            lc.Items.Clear();
            lc.Items.Add(new ListItem(FirstText, FirstValue));
            lc.DataBind();
        }
        public static void BindDataDrop(ListControl list, int iBegin, int iEnd)
        {
            list.Items.Clear();
            int count = iEnd - iBegin;
            for (int i = iBegin; i <= iEnd; i++)
            {
                list.Items.Add(i.ToString());
            }
        }
        ///
        ///根据指定值选定控件中中的项
        ///

        /// 控件名
        /// 指定值
        ///
        public static bool SelectFlg(ListControl rbl, string flg)
        {
            bool isSelect = false;
            int FlgCount = rbl.Items.Count;
            if (flg == null)
            {
                flg = "";
            }
            if (flg != null)
            {
                for (int i = 0; i <= FlgCount - 1; i++)
                {
                    if (rbl.Items[i].Value.Trim() == flg.Trim())
                    {
                        isSelect = true;
                        rbl.SelectedIndex = i;
                        break;
                    }
                }
            }
            return isSelect;
        }
        ///
        ///根据指定值选定控件中中的项
        ///

        /// 控件名
        /// 指定值
        ///
        public static void SelectFlg(ListControl rbl, DataTable dt, string ColumnName)
        {
            bool isSelect = false;
            int FlgCount = rbl.Items.Count;
            foreach (DataRow row in dt.Rows)
            {
                string flg = row[ColumnName].ToString();
                if (flg == null)
                {
                    flg = "";
                }
                if (flg != null)
                {
                    for (int i = 0; i <= FlgCount - 1; i++)
                    {                        if (rbl.Items[i].Value.Trim() == flg.Trim())
                        {
                            isSelect = true;
                            rbl.Items[i].Selected = true;                        }
                    }
                }
            }
        }
        ///
        ///根据指定值选定控件中中的项
        ///

        /// 控件名
        /// 指定值
        ///
        public static string[] GetDropSelect(ListControl rbl)
        {
            //rbl.Items
            string strValue = "";
            int FlgCount = rbl.Items.Count;
            for (int i = 0; i <= FlgCount - 1; i++)
            {
                if (rbl.Items[i].Selected)
                {
                    strValue += ";" + rbl.Items[i].Value;
                    //rbl.SelectedIndex = i;                       
                }
            }
            strValue = strValue.Remove(0, 1);
            return strValue.Split(';');
        }        ///
        ///根据指定值选定控件中中的项
        ///

        /// 控件名
        /// 指定值
        ///
        public static bool SelectFlg(ListControl rbl, object flg)
        {
            bool isSelect = false;
            int FlgCount = rbl.Items.Count;
            if (flg == null)
            {
                flg = "";
            }
            if (flg != null)
            {
                for (int i = 0; i <= FlgCount - 1; i++)
                {
                    if (rbl.Items[i].Value.Trim() == flg.ToString().Trim())
                    {
                        isSelect = true;
                        rbl.SelectedIndex = i;
                        break;
                    }
                }
            }
            return isSelect;
        }
        ///
        ///根据指定值选定控件中中的项
        ///

        /// 控件名
        /// 指定值
        ///
        public static bool SelectText(ListControl rbl, string flg)
        {
            bool isSelect = false;
            int FlgCount = rbl.Items.Count;
            if (flg == null)
            {
                flg = "";
            }
            if (flg != null)
            {
                for (int i = 0; i <= FlgCount - 1; i++)
                {
                    if (rbl.Items[i].Text.Trim() == flg.Trim())
                    {
                        isSelect = true;
                        rbl.SelectedIndex = i;
                        break;
                    }
                }
            }
            return isSelect;
        }
        ///
        /// 将字符串转换为日期
        ///

        ///
        ///
        public static DateTime ParseDateTime(string p)
        {
            try
            {
                if (p != string.Empty)
                {
                    return Convert.ToDateTime(p);
                }
                else
                {
                    return DateTime.MinValue;
                }
            }
            catch (Exception err)
            {
                return DateTime.Now;
            }
        }
   
        ///
        /// 将表中指定的一个字段构造成数组
        ///

        /// Table
        /// 指定的字段
        ///
        public static string[] AddTableColumnToArrayMethod(DataTable _SetUpedTable, string column)
        {
            string[] setUpArray = new string[_SetUpedTable.Rows.Count];
            for (int i = 0; i < setUpArray.Length; i++)
            {
                setUpArray[i] = _SetUpedTable.Rows[i][column].ToString();
            }
            return setUpArray;
        }
        ///
        /// 是否为日期型字符串
        ///

        /// 日期字符串(2008-05-08)
        ///
        public static bool IsDate(string StrSource)
        {
            return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        }        ///
        /// 是否为时间型字符串
        ///

        /// 时间字符串(15:00:00)
        ///
        public static bool IsTime(string StrSource)
        {
            return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }        ///
        /// 是否为日期+时间型字符串
        ///

        ///
        ///
        public static bool IsDateTime(string StrSource)
        {
            return Regex.IsMatch(StrSource, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
        }
        ///
        /// 在指定的目录写入文件
        ///

        /// 文件夹的路径
        /// 写入的文本内容
        /// 写入的文本文件名称
        public static void WriteFile(string driectory, string content, string fileName)
        {
            CreateDirectory(driectory);
            FileStream fs = new FileStream(driectory + fileName, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(content + "\n");
            m_streamWriter.Flush();
            m_streamWriter.Close();
            fs.Close();
        }        ///   
        /// 递归创建文件夹  
        ///
  
        /// 文件夹的路径  
        private static void CreateDirectory(string directoryName)
        {
            string sParentDirectory = Path.GetDirectoryName(directoryName);
            if (!Directory.Exists(sParentDirectory))
                CreateDirectory(sParentDirectory);
            if (!Directory.Exists(directoryName))
                Directory.CreateDirectory(directoryName);
        } 摘自红色黑客联盟(www.7747.net) 原文:http://www.7747.net/kf/201103/86637.html