常用代码:原子方法:尽可能不要嵌套跟业务有关会变动的内容

2017-08-13 09:34:18  访问(1689) 赞(0) 踩(0)


正确例子:调用方法

 
        DllInfoItem info = null;

        string preUrl = "../ViewTable/default.aspx?dll=";
        string url = null;
        string cssClass = "DefaultHref";
        string aHref = null;

        for (int i = 0; i < iCount; ++i)
        {
            rpItem = rp_List.Items[i];

            info = theList[i];

            url = preUrl + HttpUtility.UrlEncode(info.TheName);
            aHref = ch.AHrefGet(url, info.TheName, info.TitleGet(), cssClass, true);

            itxt = rpItem.FindControl(ci.lt_TheName) as ITextControl;
            itxt.Text = aHref;

        }


正确例子:原子方法


        #region 打印A.Href锚点输出

        /// <summary>
        /// 打印A.Href锚点输出
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="theName">锚点名称</param>
        /// <param name="theTitle">title提示文字</param>
        /// <param name="cssClass">样式</param>
        /// <param name="isTargetBlank">是否新窗口</param>
        /// <returns></returns>
        public string AHrefGet
            (
                string url,
                string theName,
                string theTitle,
                string cssClass,
                bool isTargetBlank
            )
        {
            StringBuilder theResult = new StringBuilder();

            theResult.Append("<a ");

            if (url != null && url.Length > 0)
            {
                theResult.Append(" href=\"" + url.Replace("\"", "&quot;") + "\"");
            }

            if (theTitle != null && theTitle.Length > 0)
            {
                theResult.Append(" title=\"" + theTitle.Replace("\"", "&quot;") + "\"");
            }

            if (cssClass != null && cssClass.Length > 0)
            {
                theResult.Append(" class=\"" + cssClass.Replace("\"", "&quot;") + "\"");
            }

            if (isTargetBlank)
            {
                theResult.Append(" target=\"_blank\"");
            }

            theResult.Append(">" + theName + "</a>");

            return theResult.ToString();
        }

        #endregion 打印A.Href锚点输出




(下面是错误的例子:把class的属性固定成class="DefaultHref"写到原子方法里面了,失去了扩展能力)

错误例子:调用方法

   
        DllInfoItem info = null;

        string preUrl = "../ViewTable/default.aspx?dll=";
        string url = null; 
        string aHref = null;

        for (int i = 0; i < iCount; ++i)
        {
            rpItem = rp_List.Items[i];

            info = theList[i];

            url = preUrl + HttpUtility.UrlEncode(info.TheName);
            aHref = ch.AHrefGet(url, info.TheName, info.TitleGet(), true);

            itxt = rpItem.FindControl(ci.lt_TheName) as ITextControl;
            itxt.Text = aHref;

        }

错误例子:原子方法

        #region 打印A.Href锚点输出

        /// <summary>
        /// 打印A.Href锚点输出
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="theName">锚点名称</param>
        /// <param name="theTitle">title提示文字</param> 
        /// <param name="isTargetBlank">是否新窗口</param>
        /// <returns></returns>
        public string AHrefGet
            (
                string url,
                string theName,
                string theTitle, 
                bool isTargetBlank
            )
        {
            StringBuilder theResult = new StringBuilder();

            theResult.Append("<a ");

            if (url != null && url.Length > 0)
            {
                theResult.Append(" href=\"" + url.Replace("\"", "&quot;") + "\"");
            }

            if (theTitle != null && theTitle.Length > 0)
            {
                theResult.Append(" title=\"" + theTitle.Replace("\"", "&quot;") + "\"");
            }

            theResult.Append(" class=\"DefaultHref\"");
            

            if (isTargetBlank)
            {
                theResult.Append(" target=\"_blank\"");
            }

            theResult.Append(">" + theName + "</a>");

            return theResult.ToString();
        }

        #endregion 打印A.Href锚点输出


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)