自定义通用HTML扩展控件

在项目中,经常用到自定义的控件.

引入命名空间 :

 using System.Web.Mvc;

  public static MvcHtmlString UIAction(this HtmlHelper helper, string value, string href, string width, string cssClass, object htmlAttributes)         {     

      StringBuilder ui = new StringBuilder();   

          ui.AppendFormat("<input"); 

          ui.AppendFormat(" type='button' value='{0}' style='width: {1}px;'", value, Regex.Replace(width, "px", "", RegexOptions.IgnoreCase));                ui.AppendFormat(" class='{0}'", cssClass);

            IDictionary<string, object> attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);          

              foreach (string key in attrs.Keys)             {                

                  ui.AppendFormat(" {0}='{1}'", key, attrs[key] as string);

             }            

  ui.AppendFormat(" />");

 return new MvcHtmlString(ui.ToString());   

      }

页面调用: @Html.UIButton("测试按钮","80","test",new {tip="tip"})

生成代码:<input class="test" style="width: 80px;" type="button" tip="tip" value="测试按钮"/>

 

 

posted @ 2012-03-12 11:23  旋转  阅读(583)  评论(0编辑  收藏  举报