• 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

  • 相关阅读:
    短url生成类
    websphere6.1部署SystemErr.log必须为元素类型“webapp”声明属性“ve
    解决websphere6.1必须为元素类型webapp声明属性version
    websphere6.1部署SystemErr.log必须为元素类型“webapp”声明属性“ve
    websphere6.1部署ear程序教程
    目测websphere6.1不支持dbcp1.4以及1.4以上版本
    maven与log4j之间的配置,log4j如何配置到web项目根目录下最简单方案
    在myeclipse中制作能部署到websphere上的java web程序教程制作ear
    【技术贴】servlet传参|前台传参含中文符号等 tomcat乱码 java后台接收乱码终极解决方
    【技术贴】servlet传参|前台传参含中文符号等 tomcat乱码 java后台接收乱码终极解决方
  • 原文地址:https://www.cnblogs.com/xulang/p/5506207.html
Copyright © 2020-2023  润新知