C#中 常用的方法

1.綁定DropDownList下拉框數據。

        /// <summary>
        /// DropDownList綁定
        /// </summary>
        /// <param name="Drop">DropDownList的名稱</param>
        /// <param name="dt">數據源 DataTable類型</param>
        /// <param name="TextField">綁定顯示字段</param>
        /// <param name="ValueField">綁定隱藏字段</param>
        /// <param name="LastStrText">option顯示字段</param>
        /// <param name="LastStrValue">option隱藏字段</param>
        public static void DropListBind(DropDownList Drop, DataTable dt, string TextField, string ValueField, string LastStrText, string LastStrValue)
        {
            BLL.t_JDKSB b_JDK = new LFMISPlat.BLL.t_JDKSB();
            Drop.DataSource = dt;
            Drop.DataTextField = TextField;
            Drop.DataValueField = ValueField;
            Drop.DataBind();
            if (LastStrText != "")
            {
                ListItem itemAll = new ListItem();
                itemAll.Text = LastStrText;
                itemAll.Value = LastStrValue;
                Drop.Items.Insert(0, itemAll);
            }
        }

  • 當LastStrText爲空字符串時,爲標準綁定。有值的時候,將值追加到DropDownList最後一個Option中。

 

2.替換過多的文本爲"......"來代替;

        /// <summary>
        /// 替換過多的文本爲“。。。。。。”
        /// </summary>
        /// <returns></returns>
        public static String ReplaceSign(string str)
        {
            if (str.Length > 10)
            {
                return str.Replace(str.Substring(10, (str.Length - (10))), "........");
            }
            else
            {
                return str;
            }
        }

例如:string str="abcdefghijklmnop"; 替換後 str="abcdefghij......";

 

 

3.替換代表的數字爲文字

        /// <summary>
        /// 替換數字爲正常或者異常
        /// </summary>
        /// <param name="strStatus"></param>
        /// <returns></returns>

        public static string GetStrChangeValue(string strStatus)
        {
            string strValue = "";

            switch (strStatus)
            {
                case "0":
                    strValue = "<font size='2'>正常</font>";
                    break;
                case "1":
                    strValue = "<font color='red' size='2'>異常</font>";
                    break;
                default:
                    break;
            }
            return strValue;
        }


 4.計算本年有多少天
       /// <summary>
        /// 計算當前年有多少天
        /// </summary>
        /// <returns></returns>
        public int DayOfYear()
        {
            DateTime firstDay = new DateTime(System.DateTime.Now.Year, 1, 1);
            DateTime lastDay = new DateTime(System.DateTime.Now.Year + 1, 1, 1);
            lastDay = lastDay.AddDays(-1);
            int dayofYear = Math.Abs(((TimeSpan)(lastDay - firstDay)).Days);
            return dayofYear;
        }




5.檢查字符串,看有沒有非法字符,不允許輸入已|分割

        #region 檢查字符串,看有沒有非法字符不允許輸入已|分割

 

        /// <summary>
        /// 檢查字符串,看有沒有非發字符不允許輸入已|分割
        /// </summary>
        /// <param name="str"></param>
        public static void check_str(string str)
        {
            string Illegal_Str = ",|&|+|'|\"|or|";
            string[] newstr = Illegal_Str.Split('|');
            for (int i = 0; i < (newstr.Length - 1); i++)
            {
                if (str.IndexOf(newstr[i]) != -1)
                {

                    System.Web.HttpContext.Current.Response.Write("<script>alert('含有非法字符!');history.back()</script>");

                }
            }
        }
        #endregion

  • 一般是在登陸的時候,驗證用戶名或密碼的時候使用。

 

6.檢查字符串,過濾or,and ',&,+,,,'',

        /// <summary>
        /// 字符串處理過濾or,and ',&,+,,,'',
        /// </summary>
        /// <returns></returns>
        public static string newstr(string str)
        {

            //過濾or,and ',&,+,,,'',
            String nstr = str.Replace("'", "").Replace("&", "").Replace(",", "").Replace("''", "");
            return nstr;
        }



7.獲取客戶端IP地址

       #region 獲取IP地址


        /// <summary>
        /// 獲訪問者的IP地址
        /// xujh2 20140902
        /// </summary>
        [WebMethod]
        public void getUserIp()
        {
            try
            {
                string result = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                //可能有代理   
                if (!string.IsNullOrWhiteSpace(result))
                {
                    //沒有"." 肯定是非IP格式  
                    if (result.IndexOf(".") == -1)
                    {
                        HttpContextRequestEnd(failedEntity.retJsonError(reErrJson.error29));
                        return;
                    }
                    else
                    {
                        //有",",估計多個代理。取第一個不是內網的IP。  
                        if (result.IndexOf(",") != -1)
                        {
                            result = result.Replace(" ", string.Empty).Replace("\"", string.Empty);
                            string[] temparyip = result.Split(",;".ToCharArray());
                            if (temparyip != null && temparyip.Length > 0)
                            {
                                for (int i = 0; i < temparyip.Length; i++)
                                {
                                    //找到不是內網的地址  
                                    if (IsIPAddress(temparyip[i]) && temparyip[i].Substring(0, 3) != "10." && temparyip[i].Substring(0, 7) != "192.168" && temparyip[i].Substring(0, 7) != "172.16.")
                                    {
                                        HttpContextRequestEnd(temparyip[i].ToString().Trim());
                                        return;
                                    }
                                }
                            }
                        }
                        //代理即是IP格式  
                        else if (IsIPAddress(result))
                        {
                            result = System.Web.HttpContext.Current.Request.UserHostAddress;
                            HttpContextRequestEnd(result.ToString().Trim());
                            return;
                        }
                        //代理中的內容非IP  
                        else
                        {
                            HttpContextRequestEnd(result.ToString().Trim());
                            return;
                        }
                    }
                }
                //如果非代理IP則獲取真正的IP
                if (string.IsNullOrWhiteSpace(result))
                {
                    result = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    HttpContextRequestEnd(result.ToString().Trim());
                    return;
                }
                //如果前面兩者都不是則獲取近似的IP地址
                if (string.IsNullOrWhiteSpace(result))
                {
                    result = System.Web.HttpContext.Current.Request.UserHostAddress;
                    HttpContextRequestEnd(result.ToString().Trim());
                    return;
                }
            }
            catch (Exception)
            {
                HttpContextRequestEnd(failedEntity.retJsonError(reErrJson.error0));
                return;
            }
            //return "Hello World";
        }


        #endregion

發佈了25 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章