Normally HTML check boxes are grouped based on name. Is it possible to group check box other开发者_开发技巧 than name?
Thank You!
Not in HTML. But you can write some code logic in the server side which groups them based on some condition, e.g. a common prefix or suffix in the parameter name or even parameter value.
However, that's only clumsy. Just profit the benefit HTML you gave with the ability to group them by just the name. If you have a problem with that, then it needs to be fixed somewhere else, maybe with the element ID (to denote uniqueness in the document tree) or the element style class (to denote a common meaning in the document tree).
If you are using ASP.NET, you can use the MutuallyExclusiveCheckBoxExtender in the AjaxToolkit to group checkboxes together. It groups them together so that only 1 checkbox in the group can be checked at a time, much like radiobutton groups.
http://www.asp.net/ajax/tutorials/creating-mutually-exclusive-checkboxes-cs
You can try sort the added items in your way. In following example for an ASP.Net Checkboxlist i used a comparer to sort the items descending(change it in your way):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim allChk As New ArrayList(Me.CheckBoxList1.Items)
allChk.Sort(New SortComparer(False))
Me.CheckBoxList1.Items.Clear()
For Each item As ListItem In allChk
Me.CheckBoxList1.Items.Add(item)
Next
End Sub
Public Class SortComparer
Implements IComparer
Private ascending As Boolean
Public Sub New(ByVal asc As Boolean)
Me.ascending = asc
End Sub
Public Function [Compare](ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Return x.ToString.CompareTo(y.ToString) * DirectCast(IIf(Me.ascending, 1, -1), Int32)
End Function
End Class
精彩评论