首页 新闻 会员 周边 捐助

如下代码如何改才能更直观,简洁,一清二楚

0
[已解决问题] 解决于 2015-10-28 15:57

主要的功能是生成表单

/// <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();
        }
菜菜灰的主页 菜菜灰 | 初学一级 | 园豆:88
提问于:2012-12-16 11:11
< >
分享
最佳答案
0

楼主你的这个代码看上去还是蛮清楚的

奖励园豆:5
chenping2008 | 大侠五级 |园豆:9836 | 2012-12-16 12:43
其他回答(1)
0

Ctrl+E+D整理一下

lanyefeng2008 | 园豆:20 (初学一级) | 2012-12-17 22:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册