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 编 ...
随机推荐
- Vant async-validator 表单校验
感谢:尤大大的 vue.有赞的 vant.async-validator.以及 asseek 链接:https://www.jianshu.com/p/d58fe749b97f 在下不才在 assee ...
- linux之 crontab 定时任务
crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查 ...
- 老齐python-基础1
1.基本对象类型 1.1数: >>> 3 #基本数字 3 >>> 3333 3333 >>> 3.222 3.222 >>&g ...
- Windows2008 R2上完全卸载Oracle操作步骤(转)
最近现场项目,碰到了好几次oracle数据库被损坏,而且无法恢复的问题,没办法,只好卸载重装了.oracle卸载确实麻烦,都是从网上查的方法, 为了方便以后查询,在此就做一下记录. Windows20 ...
- npm run dev报错,events.js:160 throw er; // Unhandled 'error' event
错误代码如下: vue-project@1.0.0 dev E:MySoftwaretestGitwebpackvue-projectnode build/dev-server.js "80 ...
- 批处理判断是否有.net环境
@echo off (echo 已安装.NET Framework) else (echo 未安装.NET Framework) pause>nul
- 【转】Jmeter:图形界面压力测试工具
Jmeter是一款强大的图形界面压力测试工具,完全用Java写成,关于Jmeter的介绍,网上其实有不少的文章,我原本是不想再重复写类似文章的,但我发现有些很关键性的,在我们测试中一定会用到的一些设置 ...
- verilog 之语法学习
1.使用非基数表示的十进制视为有符号数.使用基数表示的十进制被视为无符号数. 2.线网中的值被解释为无符号数,整型寄存器中的值被解释为有符号的二进制补码数,. 3.如果选择表达式的值为 x.z,或越界 ...
- nodejs中的util.inspect.js
util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出.它至少接受一个参数 object,即要 ...
- c语言相关知识点解析
程序基本结构 常量变量标识符 数据类型 整型类型 浮点类型(实型) 基本类型转换 字符串 函数类型 枚举类型 enum 数组类型 结构体类型 共用体类型 字符串函数 运算符 流程控制语句 输入输出语句 ...