MVC进阶学习--HtmlHelper控件解析(二)

1.InputExtensions类
      InputExtensions类主要有5种类型的扩展方法,分别用于CheckBox控件,Hidden控件,Pass控件,RadionButton控件,TextBox控件

2.CheckBox控件
      有如下重载方法:
      CheckBox(string name);
      CheckBox(string name,bool isChecked);
      CheckBox(string name,book isChecked,object htmlAttributes);
      CheckBox(string name,object htmlAttributes);
      CheckBox(string name,IDictionary<string ,object> htmlAttributes);
      CheckBox(string name,book isChecked,IDictionary<string ,object> htmlAttributes);
      例子:
      页面代码 
ContractedBlock.gif ExpandedBlockStart.gif Code
<%=Html.BeginForm("Login","Home",FormMethod.Post) %>
    
<fieldset>
        
<%=Html.CheckBox("Like1"truenew { id="MyLike1"})%>游泳<br />
        
<%=Html.CheckBox("Like2"falsenew { id="MyLike2"})%>射击<br />
        
<%=Html.CheckBox("Book"falsenew { id = "aa"})%>武侠<br/>
        
<%=Html.CheckBox("Book"falsenew { id = "bb"})%>言情<br/>
        
<%=Html.CheckBox("Book"falsenew { id = "cc"})%>小说<br/>
        
<input type="submit" value="Submit" />
    
</fieldset>
    
<%Html.EndForm(); %>
    
<br />
    
<br />
    
<%=ViewData["like1"]  %><br />
    
<%=ViewData["like2"]  %><br />
      生成页面之后的html代码如下:
      
ContractedBlock.gif ExpandedBlockStart.gif Code
<input checked="checked" id="MyLike1" name="Like1" type="checkbox" value="true" /><input name="Like1" type="hidden" value="false" />游泳<br />
        
<input id="MyLike2" name="Like2" type="checkbox" value="true" /><input name="Like2" type="hidden" value="false" />射击<br />
        
<input id="aa" name="Book" type="checkbox" value="true" /><input name="Book" type="hidden" value="false" />武侠<br/>
        
<input id="bb" name="Book" type="checkbox" value="true" /><input name="Book" type="hidden" value="false" />言情<br/>
        
<input id="cc" name="Book" type="checkbox" value="true" /><input name="Book" type="hidden" value="false" />小说<br/>

      对比上面的代码我们发现,<%=Html.CheckBox()%> 除了生产CheckBox标签之外还生成了一个隐藏的表单域,他和Checkbox具有相同的name。
      
      处理表单的aciton
ContractedBlock.gif ExpandedBlockStart.gif Code
public ActionResult Login(FormCollection formCollection)
        {
            
bool like1 = formCollection[0].Contains("true");
            
bool like2 = formCollection["Like2"].Contains("true");
            
string str = formCollection["Book"];

            ViewData[
"like1"= like1;
            ViewData[
"like2"= like2;
            
return View("Index");
        }
      通过表单集合FormCollection的索引或键,可以获取复选框的状态字符串,如果选中则得到值为"true,false",
      如果没有选中得到的值为"false",这里得到两个值是因为Checkbox还有一个对应的隐藏控件

3.RadionButton控件
      RadionButton(string name,object value);
      RadionButton(string name,object value,object htmlAttributes);
      RadionButton(string name,object value,IDictionary<string,object> htmlAttributes);
      RadionButton(string name,object value,bool isChecked);
      RadionButton(string name,object value,bool isChecked,object htmlAttributes);
      RadionButton(string name,object value,bool isChecked,IDictionary<string,object> htmlAttributes);
      这里不同Checkbox,不会生成隐藏的表单域,action中得到的值也不是True false,而是设置的值,这里不再详细讲解
ContractedBlock.gif ExpandedBlockStart.gif Code
<%=Html.RadioButton("Sex"""new { id="man"})%><br />
        
<%=Html.RadioButton("Sex"""new { id="woman"})%><br />


<input id="man" name="Sex" type="radio" value="" /><br />
        
<input id="woman" name="Sex" type="radio" value="" /><br />

3.Hidden控件
      Hidden(string name);
      Hidden(string name,object value);
      Hidden(string name,object value,object htmlAttrbutes);
      Hidden(string name,object value,IDictionary<string ,object> htmlAttributes);
      该方法生产隐藏表单域
      
4.PassWord控件
      Password(string name);
      Password(string name,object value);
      Password(string name,object value,object htmlAttrbutes);
      Password(string name,object value,IDictionary<string ,object> htmlAttributes);
      该方法生成PassWord输入框

5.TextBox控件
      TextBox(string name);
      TextBox(string name,object value);
      TextBox(string name,object value,object htmlAttrbutes);
      TextBox(string name,object value,IDictionary<string ,object> htmlAttributes);
      该方法生成普通文本输入框
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值