New for ASP.NET Web Pages: Conditional attributes
from:http://www.mikepope.com/blog/AddComment.aspx?blogid=2353
March 01, 2012
The beta release of ASP.NET Web Pages has been released (for example, as part of the ASP.NET MVC 4 Beta release). There are only a few differences from the December 2011 Developer Preview release. (Details when we've got them posted.)
A very cool feature is what's being called conditional attributes. The idea is that in markup, you can set the value of an element's attribute to a variable or expression. If the variable or expression returns false or null, the entire attribute is not rendered. There are a variety of uses for this, but it does a great job of solving a problem we inherited from HTML 1.0 (I think it was).
The problem manifests in cases where the simple appearance of an attribute name — regardless of its value — is sufficient to trigger HTML behavior. One case is the checkbox, i.e., the<input type="checkbox"> element:
<input type="checkbox" name="check1" value="check1" checked />
<input type="checkbox" name="check1" value="check1" checked="true" />
<input type="checkbox" name="check1" value="check1" checked="anyThingAtAll" />
Notice that in the first one, the checked attribute doesn't even have a value. Nonetheless, all of these work the same, namely they produce checkbox that's selected.
There's a similar situation with items in a selection list:
<select>
<option value="A">A</option>
<option value="B" selected>B</option>
<option value="C">C</option>
</select>
Technically, to select item B, the tag should read <option value="B" selected="selected">, but just including the selected attribute works.
All of this presents a problem when you want to use code in ASP.NET Web Pages to check a checkbox or select a list item. To just set a normal attribute value, you can use inline Razor code like this:
<input type="text" name="text1" value="@Request.Form["text1"]" />
But that doesn't work for the checked or selected attributes, since it doesn't matter what you set those attributes to.
The solution up to now has been to use more elaborate code in the page to render (or not render) the entire attribute, not just its value. Here's one example:
<input type="checkbox" name="check1" value="check1"
@if(Request.QueryString["val1"] == "true"){
<text>checked="checked"</text>
}
/>
Or if you were inclined, you could use the C# ternary operator, like this:
<input type="checkbox" name="check1" value="check1"
@(Request.QueryString["val1"] == "true" ? Html.Raw("checked=\"checked\"") : null)
/>
Anyway, both of these methods were a little clunky.
It's now way simpler. As I say, you can now set an attribute to a code value, and if the value is true or non-null, the attribute renders. If it's false or null, the attribute doesn't render. Here's an example:
@{
bool check1 = false;
if(Request.QueryString["val1"] == "true"){
check1=true;
}
}
Then in the markup:
<input type="checkbox" name="check1" value="check1" checked="@check1" />
Magic.
You have to be careful that you don't assume that this works for all "truthy" and "falsy" values. For example, an empty string is not a false, so you can't return "" in order to kill the attribute. You could do something like this:
<input type="checkbox" name="check1" value="check1"
checked=@(!(Request.QueryString["val1"].IsEmpty())) />
but this will render the attribute no matter what actual value ("true", "false", "foo") happens to be in the query string for val1.
Here's a page where you can see in a little more detail how this works. Pass query-string values like ?val1=true or ?val2=true to see what happens.
@{
bool checked1=false;
bool checked2=false;
Object someObject = null;
string aString = String.Empty;
if(Request.QueryString["val1"] == "true"){
checked1=true;
}
if(Request.QueryString["val2"] == "true"){
checked2=true;
someObject = this;
aString="Hello, conditional attributes!";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Test Conditional Attributes</title>
</head>
<body>
<form method="post">
<div>
<input type="checkbox" name="check1" value="check1" checked="@checked1" />
Value 1
<br/>
<input type="checkbox" name="check2" value="check2" checked="@checked2" />
Value 2
<br/>
<input type="checkbox" name="check3" value="check3" checked="@someObject" />
Some object
<br/>
<input type="checkbox" name="check4" value="check4" checked="@Request.Form["name"]" />
Request.Form["name"]
<br/>
<input type="checkbox" name="check5" value="check5" checked="@aString" />
aString
</div>
<div>
<input type="submit" name="buttonSubmit" value="Submit" />
</div>
</form>
</body>
</html>
New for ASP.NET Web Pages: Conditional attributes的更多相关文章
- Announcing the Release of ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 for VS2012
The NuGet packages for ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 are now live o ...
- ASP.NET Web Pages (Razor) API Quick Reference
ASP.NET Web Pages (Razor) API Quick Reference By Tom FitzMacken|February 10, 2014 Print This page co ...
- 如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites]
如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites] 一.布局页面介绍[Abo ...
- Displaying Data in a Chart with ASP.NET Web Pages (Razor)
This article explains how to use a chart to display data in an ASP.NET Web Pages (Razor) website by ...
- 五张图概括 什么是 ASP 、 ASP.NET (Web Pages,Web Forms ,MVC )
当你看懂下面这五张图,我相信你对于学习.NET Web开发路线将不陌生! 来源: http://www.w3 ...
- ASP.NET Web Pages 的冲突版本问题
随着VS版本和.NET MVC版本.EF的版本的不断更新,虽然很多功能随着版本的提升而更完善,但对于旧版本开发的软件就有点悲催了,或许很多开发者都遇到类似的问题! 最近有一个项目是用.NET MVC3 ...
- ASP.NET Web Pages:C# 和 VB 实例
ylbtech-.Net-ASP.NET Web Pages:C# 和 VB 实例 1.返回顶部 1. ASP.NET Web Pages - C# 和 VB 实例 通过 C# 和 Visual Ba ...
- ASP.NET Web Pages:发布网站
ylbtech-.Net-ASP.NET Web Pages:发布网站 1.返回顶部 1. ASP.NET Web Pages - 发布网站 学习如何在不使用 WebMatrix 的情况下发布 Web ...
- ASP.NET Web Pages:PHP
ylbtech-.Net-ASP.NET Web Pages:PHP 1.返回顶部 1. ASP.NET Web Pages - PHP PHP 开发人员请注意,Web Pages 可以用 PHP 编 ...
随机推荐
- Android Studio 打包及引用 AAR(可能是史上最详细的 )
https://www.jianshu.com/p/1777a634db5e Android Library(AAR) 的好处 Android 库在结构上与 Android 应用模块相同.它可以提供构 ...
- ubantu 安装tree命令
前提:必须安装好VMware tools 在linux系统中找不到tree这个命令时,需要安装,如ubuntu用下面的命令就可以安装tree这个命令工具,其他linux系统类似. 1 sudo apt ...
- 关于 SMT 一个重要提示
关于 SMT 一个重要提示 温度曲线是对应的具体的PCB的最差焊盘位置处的所用焊锡膏对应的熔化曲线.从这种角度来观察,我想你会明白我的意思的. https://www.amobbs.com/forum ...
- retful上传文件php的实现
项目中要使用restful上传文件到服务器,一直不能成功,后生成相关串后在postman中上传成功,利用这个工具生成php curl的代码,后逐步比对产生以下代码. /** * 上传文件 ...
- excel linux扩展
接近我的示例 http://ju.outofmemory.cn/entry/116399 http://tanxw.blog.51cto.com/4309543/1618576 http://blog ...
- erlang分布式例子
抄袭自 http://www.blogjava.net/killme2008/archive/2007/06/29/127099.html 简单的说,就是 主机上需要同时启用短节点名,或者长节点名 保 ...
- HTTP重要概念
connection连接 一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 在http1.1,request和reponse头中都有可能出现一个connection的头,此header的含 ...
- 支付宝pc端支付接入PHP实现
引入支付宝接口 放入一个插件库中,方便管理 创建支付类 1.发起支付 public function init() { $order_id = $_REQUEST['order_id']; $orde ...
- zabbix设置sendmail发送邮件
http://blog.csdn.net/xin_yu_xin/article/details/45115723
- Py修行路 python基础 (九)作用域 函数嵌套 闭包
名称空间与作用域 变量,函数 分成三种 #内置名称空间 内置函数, 系统函数内部自定义的. python查看内置函数,命令: import builtins dir(builtins) #全局名称空 ...