主要的功能是生成表单
/// <summary> /// TextBox /// </summary> /// <param name="name">name值</param> /// <param name="value">value值</param> /// <returns>string</returns> public static string TextBox(string name, string value, string type) { string password = "|p|password|"; string textarea = "|t|textarea|multiLine|"; if (password.Contains("|" + type + "|")) return "<input type=\"password\" id=\"password_" + name + "\" name=\"password_" + name + "\" />"; else if (textarea.Contains("|" + type + "|")) return "<textarea id=\"textarea_" + name + "\" name=\"textarea_" + name + "\">" + value + "</textarea>"; else return "<input type=\"text\" id=\"text_" + name + "\" name=\"text_" + name + "\" value=\"" + value + "\" />"; } /// <summary> /// Radio /// </summary> /// <param name="name">name值</param> /// <param name="value">value值</param> /// <param name="defaultchecked">默认值</param> /// <returns>string</returns> public static string Radio(string name, string value, string defaultchecked) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); string[] arr = value.Split('|'); for (int i = 0; i < arr.Length; i++) { if (arr[i].Contains("=")) { string[] attr = arr[i].Split('='); sb.Append("<input type=\"radio\" id=\"radio_" + name + "_" + i + "\" name=\"radio_" + name + "\" value=\"" + attr[1] + "\"" + ((defaultchecked == attr[0] || defaultchecked == attr[1] || (String.IsNullOrEmpty(defaultchecked) && i == 0)) ? " checked=\"checked\"" : "") + " /><label for=\"radio_" + name + "_" + i + "\">" + attr[0] + "</label>"); } else { sb.Append("<input type=\"radio\" id=\"radio_" + name + "_" + i + "\" name=\"radio_" + name + "\" value=\"" + arr[i] + "\"" + ((defaultchecked == arr[i] || (String.IsNullOrEmpty(defaultchecked) && i == 0)) ? " checked=\"checked\"" : "") + " /><label for=\"radio_" + name + "_" + i + "\">" + arr[i] + "</label>"); } } return sb.ToString(); } /// <summary> /// CheckBox /// </summary> /// <param name="name">name值</param> /// <param name="value">value值</param> /// <param name="defaultchecked">默认值</param> /// <returns>string</returns> public static string CheckBox(string name, string value, string defaultchecked) { if (!value.Contains("|")) { return "<input type=\"checkbox\" id=\"checkbox_" + name + "\" name=\"checkbox_" + name + "\" value=\"" + value + "\"" + ((defaultchecked == "true" || defaultchecked == value) ? " checked=\"checked\"" : "") + " />"; } else { System.Text.StringBuilder sb = new System.Text.StringBuilder(); string[] arr = value.Split('|'); for (int i = 0; i < arr.Length; i++) { if (arr[i].Contains("=")) { string[] attr = arr[i].Split('='); sb.Append("<input type=\"checkbox\" id=\"checkbox_" + name + "_" + i + "\" name=\"checkbox_" + name + "\" value=\"" + attr[1] + "\"" + ((("|" + defaultchecked + "|").Contains("|" + attr[0] + "|") || ("|" + defaultchecked + "|").Contains("|" + attr[1] + "|")) ? " checked=\"checked\"" : "") + " /><label for=\"checkbox_" + name + "_" + i + "\">" + attr[0] + "</label>"); } else { sb.Append("<input type=\"checkbox\" id=\"checkbox_" + name + "_" + i + "\" name=\"checkbox_" + name + "\" value=\"" + arr[i] + "\"" + (("|" + defaultchecked + "|").Contains("|" + arr[i] + "|") ? " checked=\"checked\"" : "") + " /><label for=\"checkbox_" + name + "_" + i + "\">" + arr[i] + "</label>"); } } return sb.ToString(); } } /// <summary> /// Select /// </summary> /// <param name="name">name值</param> /// <param name="value">value值</param> /// <param name="selected">默认值</param> /// <returns>string</returns> public static string Select(string name, string value, string selected) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<select id=\"select_" + name + "\" name=\"select_" + name + "\">"); string[] arr = value.Split('|'); for (int i = 0; i < arr.Length; i++) { if (arr[i].Contains("=")) { string[] attr = arr[i].Split('='); sb.Append("<option value=\"" + attr[1] + "\"" + ((selected == attr[0] || selected == attr[1]) ? " selected=\"selected\"" : "") + ">" + attr[0] + "</option>"); } else sb.Append("<option value=\"" + arr[i] + "\"" + (selected == arr[i] ? " selected=\"selected\"" : "") + ">" + arr[i] + "</option>"); } sb.Append("</select>"); return sb.ToString(); }
楼主你的这个代码看上去还是蛮清楚的
Ctrl+E+D整理一下