コード例 #1
0
        /// <summary>
        /// Generate custom combobox (include first element)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="lst"></param>
        /// <param name="display"></param>
        /// <param name="value"></param>
        /// <param name="firstElement"></param>
        /// <param name="attribute"></param>
        /// <param name="include_idx0"></param>
        /// <returns></returns>
        public static MvcHtmlString CommonComboBoxWithCustomFirstElement <T>(string id, List <T> lst, string display, string value, string firstElement, object attribute = null, bool include_idx0 = true) where T : class
        {
            string currentLang = CommonUtil.GetCurrentLanguage();
            string sVal        = null;

            //MessageModel all = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0120); // ---All---
            //MessageModel select = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0113); // ---Select--

            string strFirstElemText;

            if (string.IsNullOrWhiteSpace(firstElement) || firstElement.ToUpper() == COMBO_FIRSTELEMTXT_SELECT)
            {
                //strFirstElemText = CommonUtil.GetLabelFromResource("Common", "CMS030", "lblComboboxSelect");
                strFirstElemText = CommonUtil.GetLabelFromResource("Common", "CommonResources", "lblComboboxSelect");
            }
            else if (firstElement.ToUpper() == COMBO_FIRSTELEMTXT_ALL)
            {
                //strFirstElemText = CommonUtil.GetLabelFromResource("Common", "CMS030", "lblComboboxAll");
                strFirstElemText = CommonUtil.GetLabelFromResource("Common", "CommonResources", "lblComboboxAll");
            }
            else if (firstElement.ToUpper() == COMBO_FIRSTELEMTXT_CUSTOM_SELECT)
            {
                //strFirstElemText = CommonUtil.GetLabelFromResource("Common", "CMS030", "lblComboboxAll");
                strFirstElemText = CommonUtil.GetLabelFromResource("Common", "CommonResources", "lblComboboxCUSTOM_SELECT");
            }
            else if (firstElement.ToUpper() == COMBO_FIRSTELEMTXT_NONE)
            {
                //strFirstElemText = CommonUtil.GetLabelFromResource("Common", "CMS030", "lblComboboxAll");
                strFirstElemText = COMBO_FIRSTELEMTXT_CUSTOM_NONE;
            }
            else
            {
                strFirstElemText = firstElement;
            }

            var selectBuilder = new TagBuilder("select");

            selectBuilder.MergeAttribute("id", id);
            selectBuilder.MergeAttribute("name", id);

            if (attribute != null)
            {
                PropertyInfo[] prop = attribute.GetType().GetProperties();
                if (prop != null)
                {
                    if (prop.Length > 0)
                    {
                        Dictionary <string, string> dic = new Dictionary <string, string>();
                        foreach (PropertyInfo p in prop)
                        {
                            object o = p.GetValue(attribute, null);
                            if (o != null)
                            {
                                if (p.Name == "selected")
                                {
                                    //sVal = (string)o;
                                    sVal = o.ToString();
                                }
                                else
                                {
                                    //dic.Add(p.Name, (String)o);
                                    dic.Add(p.Name, o.ToString());
                                }
                            }
                        }

                        SetHtmlTagAttribute(selectBuilder, dic);
                    }
                }
            }

            if (include_idx0)
            {
                var fOptionBuilder = new TagBuilder("option");
                fOptionBuilder.MergeAttribute("value", "");
                fOptionBuilder.InnerHtml = strFirstElemText;
                selectBuilder.InnerHtml += fOptionBuilder.ToString(TagRenderMode.Normal);
            }

            if (lst != null)
            {
                foreach (T et in lst)
                {
                    PropertyInfo propD = et.GetType().GetProperty(display);
                    PropertyInfo propV = et.GetType().GetProperty(value);
                    PropertyInfo propS = et.GetType().GetProperty("Selected");

                    if (propD != null && propV != null)
                    {
                        if (propV.GetValue(et, null) != null && propD.GetValue(et, null) != null)
                        {
                            var optionBuilder = new TagBuilder("option");
                            optionBuilder.MergeAttribute("value", Encoder.HtmlEncode(propV.GetValue(et, null).ToString()));
                            optionBuilder.InnerHtml = Encoder.HtmlEncode(propD.GetValue(et, null).ToString());

                            string tt = optionBuilder.ToString(TagRenderMode.Normal);

                            if (sVal != null)
                            {
                                if (sVal == propV.GetValue(et, null).ToString())
                                {
                                    string chk = "<option ";
                                    int    idx = tt.IndexOf(chk);
                                    if (idx >= 0)
                                    {
                                        tt = tt.Substring(0, chk.Length) + "selected=\"true\" " + tt.Substring(chk.Length);
                                    }
                                }
                            }
                            if (propS != null)
                            {
                                if ((bool)propS.GetValue(et, null) == true)
                                {
                                    string chk = "<option ";
                                    int    idx = tt.IndexOf(chk);
                                    if (idx >= 0)
                                    {
                                        tt = tt.Substring(0, chk.Length) + "selected=\"true\" " + tt.Substring(chk.Length);
                                    }
                                }
                            }

                            selectBuilder.InnerHtml += tt;
                        }
                    }
                }
            }

            return(MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal)));
        }
コード例 #2
0
        public static string GenerateCSVData <T>(List <T> objDataList, bool includeRunningNo = false, bool includeHeader = true) where T : class
        {
            string       strCSVResultData       = string.Empty;
            const string C_STRING_TYPE_FULLNAME = "System.String";

            try
            {
                string        strHeaderGrid = string.Empty;
                StringBuilder sbHeaderData  = new StringBuilder();
                StringBuilder sbResultData  = new StringBuilder();
                string        strNo         = CommonUtil.GetLabelFromResource(MessageUtil.MODULE_COMMON, "CommonResources", "headerNo");

                if (objDataList != null)
                {
                    PropertyInfo[] propInfos = typeof(T).GetProperties();
                    if (propInfos != null)
                    {
                        List <PropertyInfo> propList = new List <PropertyInfo>();
                        foreach (PropertyInfo prop in propInfos)
                        {
                            CSVMappingAttribute[] objAttr = prop.GetCustomAttributes(typeof(CSVMappingAttribute), true) as CSVMappingAttribute[];
                            if (objAttr != null && objAttr.Length > 0)
                            {
                                propList.Add(prop);
                            }
                        }

                        if (propList.Count > 0)
                        {
                            propInfos = propList.ToArray();
                            Array.Sort(propInfos, delegate(PropertyInfo first, PropertyInfo second)
                            {
                                int iResult = 0;

                                CSVMappingAttribute[] objAttr1 = first.GetCustomAttributes(typeof(CSVMappingAttribute), true) as CSVMappingAttribute[];
                                CSVMappingAttribute[] objAttr2 = second.GetCustomAttributes(typeof(CSVMappingAttribute), true) as CSVMappingAttribute[];
                                if ((objAttr1 != null && objAttr1.Length > 0) && (objAttr2 != null && objAttr2.Length > 0))
                                {
                                    if (objAttr1[0] == null && objAttr2[0] != null)
                                    {
                                        iResult = -1;
                                    }
                                    else if (objAttr1[0] == null && objAttr2[0] == null)
                                    {
                                        iResult = 0;
                                    }
                                    else if (objAttr1[0] != null && objAttr2[0] == null)
                                    {
                                        iResult = 1;
                                    }
                                    else
                                    {
                                        iResult = objAttr1[0].SequenceNo.CompareTo(objAttr2[0].SequenceNo);
                                    }
                                }

                                return(iResult);
                            });

                            PropertyInfo pFirst = propInfos.First();

                            //Generate Header
                            if (includeHeader == true)
                            {
                                if (includeRunningNo)
                                {
                                    sbHeaderData.Append("\"" + strNo + "\"");
                                }
                                foreach (PropertyInfo prop in propInfos)
                                {
                                    CSVMappingAttribute[] objAttr = prop.GetCustomAttributes(typeof(CSVMappingAttribute), true) as CSVMappingAttribute[];
                                    if (objAttr == null || objAttr.Length <= 0)
                                    {
                                        continue;
                                    }

                                    if (includeRunningNo || pFirst != prop)
                                    {
                                        sbHeaderData.Append(",");
                                    }
                                    sbHeaderData.AppendFormat("\"{0}\"", String.IsNullOrEmpty(objAttr[0].HeaderName) ? prop.Name : objAttr[0].HeaderName);
                                }
                            }

                            //Generate Detail
                            int iNo = 0;
                            foreach (T objData in objDataList)
                            {
                                iNo++;

                                if (includeRunningNo)
                                {
                                    sbResultData.Append(iNo);
                                }

                                foreach (PropertyInfo prop in propInfos)
                                {
                                    CSVMappingAttribute[] objAttr = prop.GetCustomAttributes(typeof(CSVMappingAttribute), true) as CSVMappingAttribute[];
                                    if (objAttr == null || objAttr.Length <= 0)
                                    {
                                        continue;
                                    }

                                    string strValue = string.Empty;
                                    object o        = prop.GetValue(objData, null);
                                    if (o != null)
                                    {
                                        //strValue = o.ToString();
                                        //if (prop.PropertyType.FullName == C_STRING_TYPE_FULLNAME)
                                        //    strValue = String.Format("=\"\"{0}\"\"", strValue.Replace("\"", "\"\""));

                                        CSVMappingAttribute.eValueOutputFormat operationFormat;
                                        if (objAttr[0].ValueOutputFormat == CSVMappingAttribute.eValueOutputFormat.Default)
                                        {
                                            if (prop.PropertyType.FullName == C_STRING_TYPE_FULLNAME)
                                            {
                                                operationFormat = CSVMappingAttribute.eValueOutputFormat.Formula;
                                            }
                                            else
                                            {
                                                operationFormat = CSVMappingAttribute.eValueOutputFormat.Text;
                                            }
                                        }
                                        else
                                        {
                                            operationFormat = objAttr[0].ValueOutputFormat;
                                        }


                                        strValue = (o == null ? "" : o.ToString());

                                        switch (operationFormat)
                                        {
                                        case CSVMappingAttribute.eValueOutputFormat.Formula:
                                            strValue = String.Format("\"=\"\"{0}\"\"\"", strValue.Replace("\"", "\"\"\"\""));
                                            break;

                                        case CSVMappingAttribute.eValueOutputFormat.Text:
                                        default:
                                            strValue = String.Format("\"{0}\"", strValue.Replace("\"", "\"\""));
                                            break;

                                        case CSVMappingAttribute.eValueOutputFormat.Raw:
                                            if (strValue.Contains(',') || strValue.Contains('"'))
                                            {
                                                strValue = String.Format("\"{0}\"", strValue.Replace("\"", "\"\""));
                                            }
                                            break;
                                        }
                                    }

                                    //sbResultData.AppendFormat("\"{0}\",", strValue);
                                    if (includeRunningNo || pFirst != prop)
                                    {
                                        sbResultData.Append(",");
                                    }
                                    sbResultData.AppendFormat("{0}", strValue);
                                }

                                sbResultData.AppendLine();
                            }

                            if (sbResultData != null && sbResultData.Length > 0)
                            {
                                string strHeaderData = string.Empty;
                                string strResultData = sbResultData.ToString();

                                if (sbHeaderData != null && sbHeaderData.Length > 0)
                                {
                                    strHeaderData = sbHeaderData.ToString();
                                    //if (includeRunningNo)
                                    //    strHeaderData = String.Format("\"{0}\",{1}", strNo, strHeaderData);
                                }

                                strCSVResultData = String.Format("{0}{1}{2}", strHeaderData, Environment.NewLine, strResultData);
                                strCSVResultData = String.IsNullOrEmpty(strCSVResultData) ? string.Empty : strCSVResultData.Replace("<br/>", "").Replace("<BR/>", "");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(strCSVResultData);
        }
コード例 #3
0
        public static MvcHtmlString CommonComboBox <T>(string id, List <T> lst, string display, string[] value, object attribute = null, bool include_idx0 = true) where T : class
        {
            string currentLang = CommonUtil.GetCurrentLanguage();

            var selectBuilder = new TagBuilder("select");

            selectBuilder.MergeAttribute("id", id);
            selectBuilder.MergeAttribute("name", id);

            if (attribute != null)
            {
                SetHtmlTagAttribute(selectBuilder, attribute);
            }

            if (lst != null)
            {
                if (lst.Count > 0 && include_idx0)
                {
                    var fOptionBuilder = new TagBuilder("option");
                    fOptionBuilder.MergeAttribute("value", "");

                    //MessageModel select = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0113);
                    string strSelect = CommonUtil.GetLabelFromResource("Common", "CMS030", "lblComboboxSelect");

                    fOptionBuilder.InnerHtml = strSelect;

                    selectBuilder.InnerHtml += fOptionBuilder.ToString(TagRenderMode.Normal);
                }
                foreach (T et in lst)
                {
                    PropertyInfo propD = et.GetType().GetProperty(display);
                    if (propD != null)
                    {
                        if (propD.GetValue(et, null) != null)
                        {
                            bool valIsSet = false;
                            if (value != null)
                            {
                                if (value.Length > 0)
                                {
                                    valIsSet = true;
                                }
                            }

                            string valSet = string.Empty;
                            if (valIsSet)
                            {
                                foreach (string val in value)
                                {
                                    PropertyInfo propV = et.GetType().GetProperty(val);
                                    if (propV != null)
                                    {
                                        if (propV.GetValue(et, null) != null)
                                        {
                                            if (valSet != string.Empty)
                                            {
                                                valSet += ",";
                                            }
                                            valSet += propV.GetValue(et, null).ToString();
                                        }
                                    }
                                }
                            }

                            var optionBuilder = new TagBuilder("option");
                            optionBuilder.MergeAttribute("value", valSet);
                            optionBuilder.InnerHtml = propD.GetValue(et, null).ToString();

                            selectBuilder.InnerHtml += optionBuilder.ToString(TagRenderMode.Normal);
                        }
                    }
                }
            }

            return(MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal)));
        }