首页 > 代码库 > cs-HtmlHelpers

cs-HtmlHelpers

ylbtech-Unitity: cs-HtmlHelpers

 PagingHelpers.cs  SelectInputHelpers.cs TreeHelpers.cs

1.A,效果图返回顶部
 
1.B,源代码返回顶部
1.B.1,PagingHelpers.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Text;using Core.Repository;namespace Core.HtmlHelpers{    public static class PagingHelpers    {        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl, string fnData = http://www.mamicode.com/"")        {            StringBuilder result = new StringBuilder();            for (int i = 1; i <= pagingInfo.TotalPages; i++)            {                result.AppendLine("<a href=http://www.mamicode.com/‘#‘ ");//                if (i == pagingInfo.CurrentPage)                    result.Append(" class=‘selected‘ ");                result.Append( " onclick=\"pageing(this," + i + ",‘" + fnData + "‘)\">" + i + "</a>");                ////TagBuilder tag = new TagBuilder("a");    //创建<a>标签                ////tag.MergeAttribute("href", "#");//pageUrl(i));                ////tag.MergeAttribute("onclick", "pageing(this," + i + ",‘" + fnData + "‘);");                ////tag.InnerHtml = i.ToString();                //if (i == pagingInfo.CurrentPage)                //    tag.AddCssClass("selected");                //result.Append(tag.ToString() + " ");            }            return MvcHtmlString.Create("" + pagingInfo.TotalItems + "条,当前第" + pagingInfo.CurrentPage + "页,共" + pagingInfo.TotalPages + "" + result.ToString());        }    }}
View Code

1.B.2,SelectInputHelpers.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Text;using System.Collections;using System.Reflection;namespace Core.HtmlHelpers{    public static class SelectInputHelpers    {        public static MvcHtmlString SelectInput(this HtmlHelper html, string name, SelectList Options,string optionLabel=null, object htmlAttributes=null)        {            StringBuilder result = new StringBuilder("<select id=\"" + name+"\" name=\""+name+"\"");            if (htmlAttributes != null)            {                PropertyInfo[] pis = htmlAttributes.GetType().GetProperties();                foreach (PropertyInfo item in pis)                {                    result.Append("  " + item.Name + "=\"" + item.GetValue(htmlAttributes, null) + "\"");                }            }            result.Append(">");            if(optionLabel!=null)                result.AppendLine("<option value=http://www.mamicode.com/"\">" + optionLabel + "</option>");            foreach (var item in Options.Items)            {                var Value=http://www.mamicode.com/item.GetType().GetProperty(Options.DataValueField).GetValue(item, null);                var Text=item.GetType().GetProperty(Options.DataTextField).GetValue(item, null);                    if (Options.SelectedValue != null&&Value.ToString() == Options.SelectedValue.ToString())                        result.AppendLine("<option value=http://www.mamicode.com/"" + Value + "\" selected>" + Text + "</option>");                    else                        result.AppendLine("<option value=http://www.mamicode.com/"" + Value + "\">" + Text + "</option>");            }            result.AppendLine("</select>");            return MvcHtmlString.Create(result.ToString());//"共" + pagingInfo.TotalItems + "条,当前第" + pagingInfo.CurrentPage + "页,共" + pagingInfo.TotalPages + "页   " + result.ToString());        }    }}
View Code

1.B.3,TreeHelpers.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Text;using System.Collections.Specialized;using Core.WebHelper;namespace Core.HtmlHelpers{    public static class TreeHelpers    {        public static MvcHtmlString Tree(this HtmlHelper html,Tree tree )        {            IEnumerable<TreeNode> treeModels = tree.TreeNodes;            StringBuilder sb=new StringBuilder();            var root = treeModels.Where(p => p.ParentTreeNodeID == null || p.ParentTreeNodeID == "" || p.ParentTreeNodeID == "0").ToArray();            var count = root.Count();            if (count > 0)            {                if(tree.ShowLine)                    sb.AppendLine("<ul class=‘ulDashed‘>");                else                    sb.AppendLine("<ul >");                for (int i = 0; i < count; i++)// var item in root)                {                    //HtmlString += "<li>";// +item.Name;                    if (i == count - 1)                    {                        if (tree.ShowLine)                            sb.AppendLine("<li class=‘last liDashed‘>");                        else                            sb.AppendLine("<li class=‘last‘>");                    }                    else                    {                        if (tree.ShowLine)                            sb.AppendLine("<li class=‘liDashed‘>");                        else                            sb.AppendLine("<li>");                    }                    List<TreeNode> Sublist = treeModels.Where(p => p.ParentTreeNodeID == root[i].TreeNodeID).ToList();//.ModularFuns.ToList();                    int SubCount = Sublist.Count();                    #region 显示开始图标                    if (SubCount > 0)//非叶节点                    {                        if (tree.ExpentDepth > 0)                            sb.AppendLine("<span class=‘minus‘ ></span>");//+                        else                            sb.AppendLine("<span class=‘plus‘></span>");//+                        //HtmlString += "<img src=http://www.mamicode.com/‘/images/Tree/minus.gif‘/>";//展开图标                    }                    //else                    //    sb.AppendLine("<span class=‘minus‘></span>");                    //else//叶节点                    //{                    //    if (i == count - 1)                    //        HtmlString += "<span class=‘bindPic checkBoxPic‘></span>";// "<img src=http://www.mamicode.com/‘/images/Tree/line2.gif‘/>";                    //    else                    //        HtmlString += "class=‘last‘";// "<img src=http://www.mamicode.com/‘/images/Tree/line3.gif‘/>";                    //}                    #endregion                    #region 复选框                    //if(NVC["bCheck"]=="1")                    if (tree.ShowCheckBox == ShowCheckBoxs.Root || tree.ShowCheckBox == ShowCheckBoxs.All)                        sb.AppendLine("<span class=‘bindPic checkBoxPic‘></span>");                    #endregion                    #region 显示类型标记图标                    if (SubCount > 0)//非叶节点                    {                        if (tree.ExpentDepth > 0)                            sb.AppendLine("<span class=‘bindPic iconDirOpen‘></span>");//+                        else                            sb.AppendLine("<span class=‘bindPic iconDirClosed‘></span>");                        //sb.AppendLine("<span class=‘bindPic iconDirOpen‘></span>");// "<img src=http://www.mamicode.com/‘/images/Tree/folderopen.gif‘/>";//展开图标                    }                    else                    {                        sb.AppendLine("<span class=‘bindPic iconFile‘></span>");                    }                    #endregion                    #region a                    if (root[i].NodeDispType == NodeDispType.Alink)                    {                        //TagBuilder tag = new TagBuilder("a");    //创建<a>标签                        //tag.MergeAttribute("href", "#");//pageUrl(i));                        //tag.MergeAttribute("pathValue", "/" + root[i].Text);                        ////tag.MergeAttribute("onclick", "nav(‘" + root[i].Url + "‘)");//," + i + ");");                        //tag.InnerHtml = root[i].Text;                        sb.AppendLine("<a href=http://www.mamicode.com/‘#‘ " + root[i].htmlAttr + ">"+root[i].Text+"</a>");                        //sb.AppendLine(tag.ToString());                    }                    else                    {                        //sb.AppendLine("<span>"+root[i].Text +"</span>");                        sb.AppendLine("<span " + root[i].htmlAttr + "  pathValue=http://www.mamicode.com/‘/" + root[i].Text + "‘>" + root[i].Text + "</span>");                    }                    #endregion                    sb.AppendLine(BindTree(root[i], tree,0,"/"+root[i].Text));                    sb.AppendLine("</li>");                }                sb.AppendLine("</ul>");            }            MvcHtmlString mstr = new MvcHtmlString(sb.ToString());            return mstr;        }        private static string BindTree(TreeNode treeModel, Tree tree,int lev,string PathValue)        {            lev++;            IEnumerable<TreeNode> treeModels = tree.TreeNodes;            StringBuilder sb = new StringBuilder();            if (treeModel != null)            {                List<TreeNode> root = treeModels.Where(p => p.ParentTreeNodeID == treeModel.TreeNodeID).ToList();//.ModularFuns.ToList();                int count = root.Count;                if (count > 0)                {                    if (tree.ExpentDepth >= lev)                    {                        if (tree.ShowLine)                            sb.AppendLine("<ul class=‘ulDashed‘>");                        else                            sb.AppendLine("<ul >");                    }                    else                    {                        if (tree.ShowLine)                            sb.AppendLine("<ul class=hidden ulDashed‘>");                        else                        sb.AppendLine("<ul class=‘hidden‘>");                    }                    for (int i = 0; i < count; i++)// var item in root)                    {                        if (i == count - 1)                        {                            if (tree.ShowLine)                                sb.AppendLine("<li class=‘last liDashed‘>");                            else                                sb.AppendLine("<li class=‘last‘>");                            //sb.AppendLine("<li class=‘last‘>");                        }                        else                        {                           // sb.AppendLine("<li>");                            if (tree.ShowLine)                                sb.AppendLine("<li class=‘liDashed‘>");                            else                                sb.AppendLine("<li>");                        }                        List<TreeNode> Sublist = treeModels.Where(p => p.ParentTreeNodeID == root[i].TreeNodeID).ToList();//.ModularFuns.ToList();                        int SubCount = Sublist.Count();                        #region 显示开始图标                        if (SubCount > 0 )//非叶节点                        {                            //HtmlString += "<img src=http://www.mamicode.com/‘/images/Tree/minus.gif‘/>";//展开图标                            //sb.AppendLine("<span class=‘plus‘></span>");                            if (tree.ExpentDepth > lev)                                sb.AppendLine("<span class=‘minus‘></span>");//+                            else                                sb.AppendLine("<span class=‘plus‘></span>");//+                        }                        #endregion                        #region 复选框                        if ((SubCount>0&&tree.ShowCheckBox == ShowCheckBoxs.Parent)||tree.ShowCheckBox == ShowCheckBoxs.All)                            sb.AppendLine("<span class=‘bindPic checkBoxPic‘ Value=http://www.mamicode.com/‘"+root[i].Value+"‘ ></span>");                        else if ((SubCount == 0 && tree.ShowCheckBox == ShowCheckBoxs.Leaf) || tree.ShowCheckBox == ShowCheckBoxs.All)                            sb.AppendLine("<span class=‘bindPic checkBoxPic‘  Value=http://www.mamicode.com/‘" + root[i].Value + "‘></span>");                        #endregion                        #region 显示类型标记图标                        if (SubCount > 0)//非叶节点                        {                            if (tree.ExpentDepth > lev)                                sb.AppendLine("<span class=‘bindPic iconDirOpen‘></span>");//+                            else                                sb.AppendLine("<span class=‘bindPic iconDirClosed‘></span>");                        }                        else                            sb.AppendLine("<span class=‘bindPic iconFile‘></span>");//文件图标                        #endregion                        #region a                        //if (root[i].Url.Trim().Length > 0)                        //{                        //    //TagBuilder tag = new TagBuilder("a");    //创建<a>标签                        //    //tag.MergeAttribute("href", "#");//pageUrl(i));                        //    //tag.MergeAttribute("pathValue", PathValue + "/" + root[i].Text);                        //    //tag.MergeAttribute("onclick", "nav(‘" + root[i].Url + "‘)");//," + i + ");");                        //    //tag.InnerHtml = root[i].Text;                        //    sb.AppendLine(tag.ToString());                        //}                        //else                        //{                        //    sb.AppendLine("<span "+root[i].htmlAttr+" pathValue=http://www.mamicode.com/‘" + PathValue + "/" + root[i].Text + "‘>" + root[i].Text + "</span>");                        //}                        if (root[i].NodeDispType == NodeDispType.Alink)                        {                            //TagBuilder tag = new TagBuilder("a");    //创建<a>标签                            //tag.MergeAttribute("href", "#");//pageUrl(i));                            //tag.MergeAttribute("pathValue", "/" + root[i].Text);                            ////tag.MergeAttribute("onclick", "nav(‘" + root[i].Url + "‘)");//," + i + ");");                            //tag.InnerHtml = root[i].Text;                            sb.AppendLine("<a href=http://www.mamicode.com/‘#‘ " + root[i].htmlAttr + ">" + root[i].Text + "</a>");                            //sb.AppendLine(tag.ToString());                        }                        else                        {                            //sb.AppendLine("<span>"+root[i].Text +"</span>");                            sb.AppendLine("<span " + root[i].htmlAttr + "  pathValue=http://www.mamicode.com/‘/" + root[i].Text + "‘>" + root[i].Text + "</span>");                        }                        #endregion                        sb.Append(BindTree(root[i], tree, lev, PathValue+"/"+root[i].Text));                        sb.AppendLine("</li>");                        //foreach (var item in list)                        //{                        //    sb.Append("<li>");                        //    TagBuilder tag = new TagBuilder("a");    //创建<a>标签                        //    tag.MergeAttribute("href", "#");//pageUrl(i));                        //    tag.MergeAttribute("onclick", "nav(‘/" + item.ControllName + "/" + item.ActionName + "‘)");//," + i + ");");                        //    tag.InnerHtml = item.Name;                        //    sb.Append(tag.ToString());                        //    sb.Append("</li>");                        //    sb.Append(BindTree(item, treeModels));                        //}                    }                    sb.Append("</ul>");                }            }            //MvcHtmlString mstr = new MvcHtmlString(sb.ToString());            return sb.ToString();        }        //public static MvcHtmlString Tree(this HtmlHelper html, IEnumerable<ModularFun> treeModels)        //{        //    string HtmlString ="";        //    var root = treeModels.Where(p => p.ParentModularFunID == null).ToArray();        //    var count = root.Count();        //    if (count > 0)        //    {        //        HtmlString = "<ul>";        //        for (int i = 0; i < count; i++)// var item in root)        //        {        //            //HtmlString += "<li>";// +item.Name;        //            if (i == count - 1)        //            {        //                HtmlString += "<li class=‘last‘>";        //            }        //            else        //                HtmlString += "<li>";        //            List<ModularFun> Sublist = treeModels.Where(p => p.ParentModularFunID == root[i].ModularFunID).ToList();//.ModularFuns.ToList();        //            int SubCount = Sublist.Count();        //            #region 显示开始图标        //            if (SubCount > 0)//非叶节点        //            {        //                //HtmlString += "<img src=http://www.mamicode.com/‘/images/Tree/minus.gif‘/>";//展开图标        //                HtmlString += "<span class=‘minus‘></span>";        //            }        //            //else//叶节点        //            //{        //            //    if (i == count - 1)        //            //        HtmlString += "<span class=‘bindPic checkBoxPic‘></span>";// "<img src=http://www.mamicode.com/‘/images/Tree/line2.gif‘/>";        //            //    else        //            //        HtmlString += "class=‘last‘";// "<img src=http://www.mamicode.com/‘/images/Tree/line3.gif‘/>";        //            //}        //            #endregion        //            #region 复选框        //            //if(NVC["bCheck"]=="1")        //                HtmlString += "<span class=‘bindPic checkBoxPic‘></span>";        //            #endregion        //            #region 显示类型标记图标        //            if (SubCount > 0)//非叶节点        //            {        //                HtmlString += "<span class=‘bindPic iconDir‘></span>";// "<img src=http://www.mamicode.com/‘/images/Tree/folderopen.gif‘/>";//展开图标        //            }        //            else        //            {        //                HtmlString += "<span class=‘bindPic iconFile‘></span>";        //            }        //            #endregion        //            #region a        //            TagBuilder tag = new TagBuilder("a");    //创建<a>标签        //            tag.MergeAttribute("href", "#");//pageUrl(i));        //            tag.MergeAttribute("onclick", "nav(‘/" + root[i].ControllName + "/" + root[i].ActionName + "‘)");//," + i + ");");        //            tag.InnerHtml = root[i].Name;        //            HtmlString += tag.ToString();        //            #endregion        //            HtmlString += BindTree(root[i], treeModels);        //            HtmlString += "</li>";        //        }        //        HtmlString += "</ul>";        //    }        //    MvcHtmlString mstr = new MvcHtmlString(HtmlString.ToString());        //    return mstr;        //}        //private static MvcHtmlString BindTree(ModularFun treeModel, IEnumerable<ModularFun> treeModels)        //{        //    StringBuilder sb = new StringBuilder();        //    if (treeModel != null)        //    {        //        List<ModularFun> root = treeModels.Where(p => p.ParentModularFunID == treeModel.ModularFunID).ToList();//.ModularFuns.ToList();        //        int count = root.Count;        //        if (count > 0)        //        {        //            sb.AppendLine("<ul>");        //            for (int i = 0; i < count; i++)// var item in root)        //            {        //                if (i == count - 1)        //                {        //                   sb.AppendLine("<li class=‘last‘>");        //                }        //                else        //                    sb.AppendLine("<li>");        //                List<ModularFun> Sublist = treeModels.Where(p => p.ParentModularFunID == root[i].ModularFunID).ToList();//.ModularFuns.ToList();        //                int SubCount = Sublist.Count();        //                #region 显示开始图标        //                if (SubCount > 0)//非叶节点        //                {        //                    sb.AppendLine("<span class=‘minus‘></span>");//展开图标        //                }        //                //else//叶节点        //                //{        //                //    if (i == count - 1)        //                //        sb.AppendLine("<img src=http://www.mamicode.com/‘/images/Tree/lastli.gif‘/>");        //                //    else        //                //        sb.AppendLine("<img src=http://www.mamicode.com/‘/images/Tree/line3.gif‘/>");        //                //}        //                #endregion        //                #region 复选框        //                sb.AppendLine("<span class=‘bindPic checkBoxPic‘></span>");        //                #endregion        //                #region 显示类型标记图标        //                if (SubCount > 0)//非叶节点        //                {        //                    sb.AppendLine("<span class=‘bindPic iconDir‘></span>");//展开图标        //                }        //                else        //                    sb.AppendLine("<span class=‘bindPic iconFile‘></span>");//展开图标        //                #endregion        //                #region a        //                TagBuilder tag = new TagBuilder("a");    //创建<a>标签        //                tag.MergeAttribute("href", "#");//pageUrl(i));        //                tag.MergeAttribute("onclick", "nav(‘/" + root[i].ControllName + "/" + root[i].ActionName + "‘)");//," + i + ");");        //                tag.InnerHtml = root[i].Name;        //                sb.AppendLine(tag.ToString());        //                #endregion        //                sb.Append(BindTree(root[i], treeModels));        //                sb.AppendLine("</li>");        //                //foreach (var item in list)        //                //{        //                //    sb.Append("<li>");        //                //    TagBuilder tag = new TagBuilder("a");    //创建<a>标签        //                //    tag.MergeAttribute("href", "#");//pageUrl(i));        //                //    tag.MergeAttribute("onclick", "nav(‘/" + item.ControllName + "/" + item.ActionName + "‘)");//," + i + ");");        //                //    tag.InnerHtml = item.Name;        //                //    sb.Append(tag.ToString());        //                //    sb.Append("</li>");        //                //    sb.Append(BindTree(item, treeModels));        //                //}        //            }        //            sb.Append("</ul>");        //        }        //    }        //    MvcHtmlString mstr = new MvcHtmlString(sb.ToString());        //    return mstr;        //}    }}
View Code

1.B.4,

1.C,下载地址返回顶部

 

warn作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

cs-HtmlHelpers