您现在的位置是:网站首页> 编程资料编程资料
ASP.NET MVC中HtmlHelper控件7个大类中各个控件使用详解_实用技巧_
2023-05-24
295人已围观
简介 ASP.NET MVC中HtmlHelper控件7个大类中各个控件使用详解_实用技巧_
HtmlHelper类在命令System.Web.Mvc.Html之中,主要由7个静态类组成,它们分别是FormExtensions类,InputExtensions类,LinkExtensions类,SelectExtensions类,TextExtensions类,ValidationExtensions类,RenderPartialExtensions类。
为了方便开发者使用HtmlHelper控件,在视图ViewPage类中设置了一个属性Html它就是HtmlHelper类型。
一.FormExtensions类
定义了3中类型的扩展方法BeginForm,BeginRouteForm,EndForm。
(1) BeginForm (实现表单定义的开始部分)
重载方法有13个:
BeginForm();
BeginForm(Object routeValues);
BeginForm(RouteValueDictionary routeValues);
BeginForm(string actionName,string controllerName);
BeginForm(string actionName,string controllerName,object routeValues);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues);
BeginForm(string actionName,string controllerName,FormMethod method);
BeginForm(string actionName,string controllerName,object routeValues,FormMethod method);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeVaues,FormMethod method);
BeginForm(string actionName,string controllerName,FormMethod method,object htmlAttributes);
BeginForm(string actionName,string controllerName,FormMethod method,IDictionary
BeginForm(string actionName,string controllerName,object routeValues,FormMethod method,object htmlAttributes);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues,FormMethod method,IDictionary
对于第二个重载方法可以设置如下:
Html.BeginForm(new{action="action",controller="actroller",id="2"});
在上述代码中,设置了路由值的一个实例化对象,输出的HTML语句是:
对于最后一个第十三个方法的最后一个参数是实例化对象设置相关属性的值例如class,width等。
(2)BeginRouteForm (主要实现表单定义的开始部分,以路由的方法设置action的值)
有12个重载方法:
BeginRouteForm(object routeValues);
BeginRouteForm(RouteValueDictionary routeValues);
BeginRouteForm(string routeName);
BeginRouteForm(string routeName,object routeValues);
BeginRouteForm(string routeName,RouteValueDictionary routeValues);
BeginRouteForm(string routeName,FormMethod method);
BeginRouteForm(string routeName,object routeValues,FormMethod method);
……
对于第一个重载方法:
Html.BeginRouteForm(new {action="action"});
Home是页面所在的目录
BeginForm与BeginRouteForm的区别就在于第一个的action是action第二个的action是Home/action
(3)EndForm(实现表单的定义的结束部分)
Html.EndForm();
相当于
二.InputExtensions类有5种类型的扩展方法,可在视图中设置checkBox,hidden,password,radioButton,textBox控件。
(1)CheckBox 实现复选框控件有6个重载方法
CheckBox(string name);
CheckBox(string name,bool isChecked);
CheckBox(string name,bool isChecked,object htmlAttributes);
CheckBox(string name,object htmlAttributes);
CheckBox(string name,Idictionary
CheckBox(string name,bool isChecked,Idictionary
设置复选框的实现代码:
<%=Html.BeginForm("CheckBox","Home") %>
<%Html.EndForm();%>
运行上述代码,上述复选框的设置代码对应的HTML语句:
在后台检索checkBox
public ActionResult CheckBox (FormCollection formCollection)
{
bool MyCheckBox1=formCollection[0].Contains("true");//检索第一个复选框是否被选中
bool MyCheckBox2=formCollection["MyCheckBox2"].Contains("true");//检索名字是MyCheckBox2的复选框是否倍选中
ViewData["CheckBox1"]=MyCheckBox1;
ViewData["CheckBox2"]=MyCheckBox2;
return View();
}
(2)Hidden 表单中的隐藏数值,有4个重载方法。
Hidden(string name);
Hidden(string name,object value);
Hidden(string name,object value,object htmlAttributes);
Hidden(string name,object value,Idictionary
eg:
Html.Hidden("testName");
对应输出的Html语句如下:
(3)Password 主要是输入密码的文本框,有4个重载方法。
Hidden(string name);
Password (string name,object value);
Password (string name,object value,object htmlAttributes);
Password (string name,object value,Idictionary
eg:
Html.Password ("MyPwd");
对应输出的Html语句如下:
--------------------------------------------------------------------------------------------
HTML扩展类的所有方法都有2个参数:
以textbox为例子
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, IDictionary
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, Object htmlAttributes )
这2个参数代表这个html标签的属性集合。使用方法如下。
1.ActionLink
<%=Html.ActionLink("这是一个连接", "Index", "Home")%>
带有QueryString的写法
<%=Html.ActionLink("这是一个连接", "Index", "Home", new { page=1 },null)%>
<%=Html.ActionLink("这是一个连接", "Index", new { page=1 })%>
有其它Html属性的写法
<%=Html.ActionLink("这是一个连接", "Index", "Home", new { id="link1" })%>
<%=Html.ActionLink("这是一个连接", "Index",null, new { id="link1" })%>
QueryString与Html属性同时存在
<%=Html.ActionLink("这是一个连接", "Index", "Home", new { page = 1 }, new { id = "link1" })%>
<%=Html.ActionLink("这是一个连接", "Index" , new { page = 1 }, new { id = "link1" })%>
生成结果为:
带有QueryString的写法
有其它Html属性的写法
QueryString与Html属性同时存在
2.RouteLink
跟ActionLink在功能上一样。
<%=Html.RouteLink("关于", "
相关内容
- ASP.NET使用HttpWebRequest读取远程网页源代码_实用技巧_
- ASP.NET过滤类SqlFilter,防止SQL注入[原创]_实用技巧_
- 基于.NET平台常用的框架和开源程序整理_自学过程_
- ASP.NET在底层类库中获取Session C#类中获取Session[原创]_实用技巧_
- GridView导出Excel常见的5种文本格式[原创]_实用技巧_
- 全面剖析.Net环境下的缓存技术_实用技巧_
- ADO.NET通用数据库访问类_实用技巧_
- ASP.NET MVC异常处理模块详解_实用技巧_
- ASP.NET/C#中如何调用动态链接库DLL_实用技巧_
- asp.net实现调用存储过程并带返回值的方法_实用技巧_
点击排行
本栏推荐
