473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to precheck the checkbox of Bound CheckBoxList?

Hi All,

Below is my SQL Statement,

Select Text, Value, Status from Tables

I want to mark as a checked when the Status is true, how do I achieve this
during databinding?

Thank you very much.

Regards,
Oshiko
Apr 20 '06 #1
4 2809
I don't think you can do this with a CheckBoxList. You might want to
use a Repeater, or a DataList.

<asp:DataList ID="DataList1" runat="server" DataKeyField="Value">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1"
runat="server" Checked='<%# Eval("Status") %>' Text='<%# Eval("Text")
%>' />
</ItemTemplate>
</asp:DataList>

Apr 20 '06 #2
MS
Hi
you need to do it urself... either by looping through List items .... Like

for( ..... to Listitems lenth......)
{
list.items.add(new listitem(datatable.rows[i][colname].....)
if(datatable.rows[i][colname] == '1')
{
list.items[i].selected=true;
}
}

best ofluck.. Munawar Hussain

"oshiko" <os****@microlink.com.my> wrote in message
news:ub**************@TK2MSFTNGP02.phx.gbl...
Hi All,

Below is my SQL Statement,

Select Text, Value, Status from Tables

I want to mark as a checked when the Status is true, how do I achieve this
during databinding?

Thank you very much.

Regards,
Oshiko

Apr 20 '06 #3
Oshiko,

I don't think you can do it during databinding. You can rather loop through
the dataset and set the Selected property for the items depending on the
Status column value.

Eliyahu

"oshiko" <os****@microlink.com.my> wrote in message
news:ub**************@TK2MSFTNGP02.phx.gbl...
Hi All,

Below is my SQL Statement,

Select Text, Value, Status from Tables

I want to mark as a checked when the Status is true, how do I achieve this
during databinding?

Thank you very much.

Regards,
Oshiko

Apr 20 '06 #4
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.Inheritance Demand, _
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.Curre nt,
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

Apr 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Francois Verbeeck | last post by:
Dear UseNet readers, Does anyone have any idea on how to colorize selected checkbox in checkboxlist control ? I've quite a huge checkboxlist (approximatively one full screen) and, to improve...
9
by: Harry | last post by:
Dear All, First of all, i have a database and i have to select the data in a table. In the web form, i have a checkbox in each rows. So that the use can select the row. initally, i am...
5
by: DotNetJunkies User | last post by:
1. i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis.......
2
by: Patrick.O.Ige | last post by:
I know with CheckBox i can get ChecBox1.Checked which gives me TRUE or FALSE.. But i want to DataBind with CheckBoxList but is it possible to get TRUE or FALSE result with CheckBoxList . And also...
1
by: segue | last post by:
I'm dynamically creating/populating a checkbox list and adding it to a web form. I want to when checking an item in the list have the autopostback retrieve the selected item. I'm dynamically...
7
by: ThunderMusic | last post by:
Hi, I have a CheckBoxList and I want to add some javascript code to each CheckBox created by this CheckBoxList. I tried iterating through all items of the list, all the controls, do a FindControl,...
6
by: Chaprasi | last post by:
Gurus of asp.net and C# Please Help! I have a checkbox list solution to implement. This is my code <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="3"...
3
by: cannontrodder | last post by:
I am displaying names and other details of my users in a Formview control by binding my custom business object to it. My custom object also has a property that is a collection of boolean values and...
3
by: | last post by:
Hi all, I have a CheckBoxList control which has about 10 items. I have set autopostback=true and also set an eventhandler for OnSelectedIndexChanged. The problem is I want to identify which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.