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))); }
/// <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))); }
/// <summary> /// Convert list to xml /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lst"></param> /// <param name="file"></param> /// <param name="type"></param> /// <returns></returns> public static string ConvertToXml <T>(List <T> lst, string file = null, GRID_EMPTY_TYPE type = GRID_EMPTY_TYPE.VIEW) where T : class { try { XmlDocument doc = new XmlDocument(); doc.LoadXml("<response></response>"); #region Load Header int totalCol = 0; try { if (file != null) { string file_code = string.Empty; string[] pt = file.Split("\\".ToCharArray()); if (pt.Length > 0) { string txt_p = pt[pt.Length - 1]; if (txt_p != string.Empty) { txt_p = txt_p.Substring(0, 6); } if (pt.Length > 1) { for (int i = 0; i <= pt.Length - 2; i++) { if (file_code != string.Empty) { file_code += "\\"; } file_code += pt[i]; } if (file_code != string.Empty) { file_code += "\\"; } } file_code += txt_p; } string lang = CommonUtil.GetCurrentLanguage(); if (lang == ConstantValue.CommonValue.DEFAULT_LANGUAGE_EN) { lang = string.Empty; } else { lang = "." + lang; } string resourcePath = string.Format("{0}{1}\\{2}{3}.resx", CommonUtil.WebPath, ConstantValue.CommonValue.APP_GLOBAL_RESOURCE_FOLDER, file_code, lang); XmlDocument rDoc = new XmlDocument(); rDoc.Load(resourcePath); string filePath = string.Format("{0}{1}\\{2}.xml", CommonUtil.WebPath, ConstantValue.CommonValue.GRID_TEMPLATE_FOLDER, file); XmlDocument hDoc = new XmlDocument(); hDoc.Load(filePath); if (hDoc.ChildNodes.Count >= 2) { doc.ChildNodes[0].InnerXml = hDoc.ChildNodes[1].InnerXml; //--- Get total "column" node, and update column header ---// foreach (XmlNode node in doc.ChildNodes[0].ChildNodes[0].ChildNodes) { if (node.Name == "column") { if (node.InnerText != "#cspan" && node.InnerText != "#rspan") { XmlNode rNode = rDoc.SelectSingleNode(string.Format("root/data[@name=\"{0}\"]/value", node.InnerText)); if (rNode != null) { node.InnerText = rNode.InnerText; } else if (node.InnerText.TrimStart(' ').StartsWith("<")) { //Do nothing } else { node.InnerText = " "; } } totalCol += 1; } else if (node.Name == "afterInit") { foreach (XmlNode aNode in node.ChildNodes) { if (aNode.Name == "call" && aNode.Attributes["command"] != null) { if (aNode.Attributes["command"].Value == "attachHeader") { foreach (XmlNode pNode in aNode.ChildNodes) { if (pNode.InnerText != null) { string[] txts = pNode.InnerText.Split(",".ToCharArray()); foreach (string txt in txts) { if (txt != "#cspan" && txt != "#rspan") { XmlNode rNode = rDoc.SelectSingleNode(string.Format("root/data[@name=\"{0}\"]/value", txt)); if (rNode != null) { pNode.InnerText = pNode.InnerText.Replace(txt, rNode.InnerText); } else { pNode.InnerText = pNode.InnerText.Replace(txt, " "); } } } } } } } } } } } } } catch { } #endregion #region Initial Grid XmlNode rowsNode = doc.CreateNode(XmlNodeType.Element, "rows", ""); doc.ChildNodes[0].AppendChild(rowsNode); bool isRowMoreMaximum = false; if (lst != null) { if (lst.Count > ConstantValue.CommonValue.MAX_GRID_ROWS && type == GRID_EMPTY_TYPE.SEARCH) { isRowMoreMaximum = true; } } MessageModel msg = null; if (isRowMoreMaximum) { msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0052, ConstantValue.CommonValue.MAX_GRID_ROWS.ToString("N0")); } else if (type == GRID_EMPTY_TYPE.SEARCH) { msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0001); } else if (type == GRID_EMPTY_TYPE.INSERT) { msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0110); } else { msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0109); } rowsNode.InnerXml = string.Format("<userdata name=\"msgdata\">{0}</userdata>", msg.ToJson); #endregion #region Set XML if (isRowMoreMaximum == false && lst != null) { Type cType = typeof(T); Dictionary <string, FixedGridToolTipAttribute> dicAttrFixedToolTip = CreateAttributeDictionary <FixedGridToolTipAttribute>(typeof(T)); Dictionary <string, GridToolTipAttribute> dicAttrToolTip = CreateAttributeDictionary <GridToolTipAttribute>(typeof(T)); Dictionary <string, string> dictNumberFormat = GetNumberFormatList(doc); PropertyInfo[] props = cType.GetProperties(); foreach (T obj in lst) { XmlNode node = doc.CreateNode(XmlNodeType.Element, cType.Name, ""); rowsNode.AppendChild(node); foreach (PropertyInfo prop in props) { XmlNode iNode = doc.CreateNode(XmlNodeType.Element, prop.Name, ""); if (dicAttrFixedToolTip.ContainsKey(prop.Name)) { XmlAttribute attrTitle = doc.CreateAttribute("title"); attrTitle.Value = dicAttrFixedToolTip[prop.Name].ToolTipText; iNode.Attributes.Append(attrTitle); } else if (dicAttrToolTip.ContainsKey(prop.Name)) { XmlAttribute attrTitle = doc.CreateAttribute("title"); PropertyInfo propToolTip = cType.GetProperty(dicAttrToolTip[prop.Name].PropertyName); if (propToolTip != null) { attrTitle.Value = CommonUtil.GetXmlValue <T>(obj, propToolTip); iNode.Attributes.Append(attrTitle); } } if (dictNumberFormat.ContainsKey(prop.Name)) { XmlAttribute attrNumberFormat = doc.CreateAttribute("numberformat"); attrNumberFormat.Value = dictNumberFormat[prop.Name]; iNode.Attributes.Append(attrNumberFormat); } string val = CommonUtil.GetXmlValue <T>(obj, prop); iNode.InnerText = val; node.AppendChild(iNode); } } } #endregion #region Check Data is exist? //Add Data not found row. //XmlNode nnode = doc.CreateNode(XmlNodeType.Element, "rows", ""); //doc.ChildNodes[0].InsertBefore(nnode, doc.ChildNodes[0].FirstChild); //MessageModel msg = null; //if (isRowMoreMaximum) // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0052); //else if (type == GRID_EMPTY_TYPE.SEARCH) // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0001); //else if (type == GRID_EMPTY_TYPE.INSERT) // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0110); //else // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0109); //nnode.InnerXml = string.Format("<userdata name=\"msgdata\">{0}</userdata>", // msg.ToJson); //if (doc.ChildNodes[0].ChildNodes.Count == 0 // || isRowMoreMaximum == true // || ((file != null) && doc.ChildNodes[0].ChildNodes.Count == 1)) //{ // //Add Data not found row. // XmlNode node = doc.CreateNode(XmlNodeType.Element, "row", ""); // doc.ChildNodes[0].AppendChild(node); // if (totalCol == 0) // totalCol = 1; // MessageModel msg = null; // if (isRowMoreMaximum) // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0052); // else if (type == GRID_EMPTY_TYPE.SEARCH) // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0001); // else if (type == GRID_EMPTY_TYPE.INSERT) // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0110); // else // msg = MessageUtil.GetMessage(MessageUtil.MODULE_COMMON, MessageUtil.MessageList.MSG0109); // node.InnerXml = string.Format("<cell colspan=\"{0}\" rowspan=\"1\" align=\"left\">{1}</cell>", // totalCol, // msg.ToJson); //} StringWriter sw = new StringWriter(); XmlTextWriter tx = new XmlTextWriter(sw); doc.WriteTo(tx); string xml = sw.ToString(); #endregion return(xml); } catch (Exception) { throw; } }