支持 Value 属性的 .NET CheckBox 控件
我知道默认的 .NET CheckBox 控件不支持 Value 属性。 这对我来说似乎很奇怪,因为输入控件确实支持将值作为 html 规范的一部分。
所以我的问题是这里是否有人知道 ASP.NET 的自定义用户控件,其行为类似于标准 ASP .NET CheckBox 和 CheckBoxList?
I am aware that the default .NET CheckBox control doesn't support the Value property. This seems weird to me because the input control does support value as part of the html spec.
So my question is whether or not anyone here knows of a custom user control out there for asp.net that acts similar to the standard ASP .NET CheckBox and CheckBoxList?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
也许我误解了你的问题,但你知道你也可以使用典型的 HTML 控件吗?
例如,创建一个 ASPX 页面并添加以下源代码:
然后在后面的代码中添加以下代码:
您可以将 html 复选框值属性设置为您喜欢的任何值。
Perhaps I'm misunderstanding your question but are you aware that you can use the typical HTML controls as well?
For example create an ASPX page and add the following source:
Then in your code behind add the following code:
You can set the html check box value property to whatever you like.
如果您只需要在服务器端访问它,则可以轻松创建一个继承 Checkbox 控件的服务器控件,并向其添加一个名为 value 的属性,该属性可以从服务器端设置和检索。
但是,如果您需要从客户端访问它,则必须对服务器控件呈现的内容发挥想象力 - 可能是复选框和隐藏字段的组合,其中隐藏字段包含值。
如果您所做的只是在服务器端提取复选框的值,那么我想复选框控件的简单扩展就足够了 - 添加一个可以从服务器端设置和检索的额外属性。 。 在公园里散步。
If you only need access to it on the server side, you can easily create a server control that inherits the Checkbox control and add a property to it called value which can be set and retrieved from the server side.
If you need to access it from the client side however, you're gonna have to get imaginitive with what the server control renders - likely a combination of a checkbox and hidden field whereby the hidden field contains the value.
If all you're doing is extracting the value of the checked boxes on the server side, I would imagine that a simple extension of the checkbox control would suffice though - add an extra property that can be set and retrieved from the server side... walk in the park.
不,它们不一样
我正在开发的功能需要将产品升级列表显示为复选框列表。 根据“选中”的复选框,我需要将它们添加到产品中。 CheckBoxList 中的项目是动态的,因此当我迭代选中的项目时,我需要某种唯一标识符。 “Checked”值只是一个布尔值
No they are not the same
The feature I am working on requires a list of product upgrades to be shown as a list of CheckBoxes. Based on which checkboxes are "checked" i will need to add them to the product. The items in the CheckBoxList are dynamic so when i iterate over the checked items I need some sort of unique identifier. The "Checked" value is only a Boolean
MS 在这里所做的是创建具有 .net 感觉的 ASP 控件。 但是,由于它们应该与浏览器一起使用,因此它们呈现为标准 HTML 控件。
因此,在 cs/vb 代码隐藏中,您会看到 .Checked (bool),而在 client/javascript 中您会看到 .value。
如果您想要的是唯一标识符,那么您需要查看 ID 或 ClientID。 或者,您可以向复选框添加一个属性 (.Attributes.Add()) 并使用它。
What MS has done here is created their ASP controls with a .net feel to them. However, since they're supposed to work with browsers, they render as standard HTML controls.
So in the code-behind cs/vb, you see .Checked (bool), while in the client/javascript you see .value.
If what you want is a unique identifier, then you need to be looking at ID or ClientID. Alternatively, you could add an attribute to the checkboxt (.Attributes.Add()) and use that.
看起来这些值没有在 html 中呈现,但仍然存储在视图状态中。 因此,当我访问 CheckboxList.Items() 中的 ListItem 对象时,我能够在服务器端提取值
It seems that the values are not rendered in the html but are still stored in the viewstate. So i was able to pull the value on the server side when i accessed the ListItem object in the CheckboxList.Items()
您可以通过重写 CheckBox 创建自己的自定义控件。 这是我刚刚创建的一个示例,用于添加可用服务器和客户端的 Value 属性:
You can create your own custom control by overriding CheckBox. Here's an example I just created to add a Value property available server and client side:
这是一个老问题,但情况已经发生了变化。 首先,在asp.net 4.6中,将在复选框上设置value属性。 这些答案很少与 CheckBoxList 相关,它使用 ListItem 类来生成复选框。
我对 4.6 之前版本的解决方案是添加一个数据属性。 这会在复选框和标签周围创建一个
控件,您可以使用 javascript 找到该控件。
This is an old question, but things have changed. First, in asp.net 4.6, the value attribute will be set on the checkbox. Few of this answers are relevant to CheckBoxList, which uses the ListItem class to generate checkboxes.
My solution for pre-4.6 is to add a data attribute. This creates a
<span>
control around the checkbox and label which you can find with javascript.“检查”和“值”不一样吗?
http://www.w3schools.com/aspnet/control_checkbox.asp
看一下在那里。 您实际上并不需要专门命名为“Value”的属性,因为“Checked”将为您提供相同的信息。
Isn't "Checked" the same as "Value"?
http://www.w3schools.com/aspnet/control_checkbox.asp
Take a look in there. You don't really need a property specifically named "Value", because "Checked" will give you the same information.