博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
New for ASP.NET Web Pages: Conditional attributes
阅读量:5039 次
发布时间:2019-06-12

本文共 3385 字,大约阅读时间需要 11 分钟。

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 ). 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:

  

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:

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:

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:

checked="checked"    } />

Or if you were inclined, you could use the , like this:

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:

Magic.
You have to be careful that you don't assume that this works for all . 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:

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!"; } }
Test Conditional Attributes
Value 1
Value 2
Some object
Request.Form["name"]
aString

转载于:https://www.cnblogs.com/imust2008/p/4986708.html

你可能感兴趣的文章
sqlserver in 和 exist 子查询
查看>>
bzoj 2730: [HNOI2012]矿场搭建
查看>>
本地及远程二级缓存
查看>>
机器学习(周志华)——学习笔记2
查看>>
使用ERStudio创建数据表与ER图
查看>>
ubuntu防火墙设置
查看>>
100步问题
查看>>
PLSQL Persistent State文摘
查看>>
对PostgreSQL中bufmgr.c的进一步学习
查看>>
PostgreSQL在何处处理 sql查询之四十四
查看>>
STL: reverse
查看>>
2017-10-15 NOIP模拟赛
查看>>
[学习笔记]设计模式之Proxy
查看>>
Asp.net MVC 中Ajax的使用 [分享]
查看>>
SQL SERVER 2008数据库的表中修改字段的数据类型后,不能保存
查看>>
java基础 (四)之集合
查看>>
单页网站不是梦,几款国外的单页网站创建工具
查看>>
分享30个高品质的抽象网页背景素材
查看>>
NavBarControl 左侧菜单
查看>>
转-设置/使用反向代理服务器
查看>>