为什么我不能设置一个asp的一个值:复选框? [英] Why I can't set a value on a asp:CheckBox?
问题描述
没有属性值
:
<asp:CheckBox ID="CheckBox1" runat="server" />
而在标准的HTML,这是允许的:
while on standard HTML this is allowed :
<input type="checkbox" ID="CheckBox1" value="My Valyue" />
为什么?
推荐答案
像所有的ASP.NET控件,它有一个文本
属性。
Like all ASP.NET controls, it has a Text
property.
在呈现时,它被翻译成值
属性。
When rendered, it gets translated to a value
attribute.
更新 - 上面是完全错误的......在文本
属性用于渲染的复选框的标签
Update - the above is completely wrong... the Text
property is used to render a label for the checkbox.
控制器有一个<一个href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.inputattributes.aspx"><$c$c>InputAttributes$c$c>属性,你可以添加到:
The control has an InputAttributes
property that you can add to:
myChk.InputAttributes.Add("value", "My Value");
我相信,如果你只需添加值
属性标记,这也将得到填充。
I believe that if you simply add the value
attribute to the markup, this will also get populated.
您可以访问值,像这样:
You can access the value like so:
myChk.InputAttributes["value"];
要回答的问题的为什么的值
不是一个建立在属性添加到复选框
控制:
To answer the question of why Value
is not a build in attribute to the CheckBox
control:
A 复选框
单独(就其本身),需要的不是价值。根据定义,它是一个布尔值,是由它的ID来标识。所有你需要做的是检查是否被选中。
A CheckBox
in isolation (just by itself) needs no value. By definition it is a boolean and is identified by its ID. All you need to do is check whether it was checked or not.
值进场时,你的集团的复选框,并没有为该控制 - 的<一个href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx%29."><$c$c>CheckBoxList$c$c>使用<一个href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.aspx"><$c$c>ListItem$c$c> - 每个列表项
确实有一个值
属性
The value comes into play when you group checkboxes and there is a control for that - the CheckBoxList
that uses ListItem
- each ListItem
does have a Value
property.
这篇关于为什么我不能设置一个asp的一个值:复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!