Here is a chontrol i made to handle this problem. You need to have
your datasource provide a field with as a checked/unchecked value when
databinding. You set the DataCheckedField value to the name of this
field. So you may have to handle your select statements a little
different than normal. Here is the source althought, feel free to mail
me if you have questions. This control is derived from checkboxlist
control.
This is control is quiered off the selected value of a gridview.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal), _
AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal), _
DefaultProperty("Text"), _
ToolboxData("<{0}:MegaCheckBoxList
runat=server></{0}:MegaCheckBoxList>")> _
Public Class MegaCheckBoxList
Inherits System.Web.UI.WebControls.CheckBoxList
Private _checklist As New ArrayList
'---------------------------------------------------
' PROPERTIES
'---------------------------------------------------
<Category("Data"), DefaultValue(""),
TypeConverter(GetType(String))> _
Public Property DataCheckedField() As String
Get
Dim o As Object
o = ViewState("DataCheckedField")
Return o
End Get
Set(ByVal value As String)
ViewState("DataCheckedField") = value
End Set
End Property
<Category("Apperance"), DefaultValue(""),
TypeConverter(GetType(String))> _
Public Property CheckedCssClass() As String
Get
Dim o As Object
o = ViewState("CheckedCssClass")
Return o
End Get
Set(ByVal value As String)
ViewState("CheckedCssClass") = value
End Set
End Property
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
Try
For i As Integer = 0 To Items.Count() - 1
If _checklist(i) Then
CType(MyBase.Items(i), ListItem).Selected = True
End If
Next
Catch
End Try
End Sub
Public Sub CheckAll()
For Each item As ListItem In Items
item.Selected = True
Next
End Sub
Protected Overrides Sub RenderItem(ByVal itemType As
System.Web.UI.WebControls.ListItemType, ByVal repeatIndex As Integer,
ByVal repeatInfo As System.Web.UI.WebControls.RepeatInfo, ByVal writer
As System.Web.UI.HtmlTextWriter)
If Me.Items(repeatIndex).Selected Then
writer.Write("<div class='" & CheckedCssClass & "'>")
MyBase.RenderItem(itemType, repeatIndex, repeatInfo,
writer)
writer.Write("</div>")
Else
MyBase.RenderItem(itemType, repeatIndex, repeatInfo,
writer)
End If
End Sub
Protected Overrides Sub PerformDataBinding(ByVal dataSource As
System.Collections.IEnumerable)
Try
MyBase.PerformDataBinding(dataSource)
Dim e As IEnumerator = dataSource.GetEnumerator
While e.MoveNext()
_checklist.Add(DataBinder.GetPropertyValue(e.Current,
DataCheckedField))
End While
Catch
End Try
End Sub
End Class
----------------------------------------------
I use the control something like this.
<MCS:MegaCheckBoxList ID="chklstMapping" runat="server"
DataSourceID="sqldsBulletinDepartment"
Width="100%"
RepeatColumns="3"
RepeatLayout="Table"
DataTextField="DepartmentName"
DataValueField="DepartmentID"
DatacheckedField="Checked"
CssClass="CleanDetailsView" CellPadding="5" CellSpacing="5"
DataTextFormatString=" {0}"
Enabled="True"
CheckedCssClass="CheckedItems" >
</MCS:MegaCheckBoxList>
---------------------------------
<asp:SqlDataSource ID="sqldsBulletinDepartment" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString%>"
SelectCommand="sp_GetDepartmentsByBulletinID"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="gvBulletins"
Name="BulletinID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
--------------------------------------------
And my Stored Procedure looks like
-- =============================================
-- Author: Andrew Boudreau
-- Create date: 3/14/2006
-- Description: Gets the departments for a given bulletin with
-- Checked column set to true. This is used for megacheckbox.
-- =============================================
ALTER PROCEDURE [dbo].[sp_GetDepartmentsByBulletinID]
@BulletinID INT
AS
BEGIN
SET NOCOUNT ON;
SELECT D.DepartmentID, D.DepartmentName,
CASE
WHEN (SELECT COUNT(*)
FROM DepartmentToBulletin DTB
WHERE DTB.DepartmentID = D.DepartmentID AND
DTB.BulletinID = @BulletinID) > 0 THEN 1
ELSE 0
END as Checked
FROM Department AS D
ORDER BY D.DepartmentName
END
So, that should give you an idea of everything you need for a
pre-checked checkbox list that is bindable. Good luck, let me know if
you have questions. Ohh yeah, the class doesn't really have any good
error handling but it seems to work for now. The real trick on the
databinding comes into play in Protected Overrides Sub
PerformDataBinding(ByVal dataSource As
System.Collections.IEnumerable) which allows us to easily change the
way the datafield is binding, plus this can work with datasourceid or
datasource. Pretty sweet, thanks .net 2.0! I know you're suppose to
use html writers and what not but i'm still learning, so go easy on me
please.
Thanks,
Andrew