Getting Databinded CheckBoxlist to display Boolean value ?

P

Patrick.O.Ige

I'm DataBinding a CheckBoxList and i want to get the checkboxes selected
when the page is loaded depending on a Boolean value from the Database..
chkDebtor.DataSource = objDR
chkDebtor.DataValueField = "checked" -- this is a column in the
table that returns 1 or 0
chkDebtor.DataTextField = "DebtorCode"
chkDebtor.DataBind()

Dim i As Integer = 0
While i < chkDebtor.Items.Count
chkDebtor.Items(i).Selected =
Microsoft.VisualBasic.IIf((chkDebtor.Items(i).Value = "1"), True, False)
System.Math.Min(System.Threading.Interlocked.Increment(i),
i - 1)
End While

The above code works
But i have some problems I need another DataValueField which is
"DebtorCodeID" but since i need the boolean value i used
the chkDebtor.DataValueField = "checked"
I need the "DebtorCodeID" later in my code.
My question is what other ways can i loop through the checkblxlist without
setting the DataValueField = "checked" but to
DataValueField = "DebtorCodeID"
Is there a way to get the Checked boolean values and pass it to the
checkboxlist to get False or TRUE?
 
G

Guest

I think, A while back I faced a similar problem and I found it easier to use
a Repeater control with checkboxes in it instead of a CheckBoxList

While using Repeater control, you will be able to easily achive this with the
ItemDataBound event..

pls let me know , if you need help in implementing this with Repeater

Sreejith
 
P

Patrick.O.Ige

Yes i think i would need to look at Repeater then.
It would be nice if you can provide me some info.
Thx
 
G

Guest

A DATALIST you need, would look like below
*****************************************
<asp:DataList id="DataList1" RepeatDirection="Horizontal"
RepeatLayout="Table" RepeatColumns="3"
runat="server">
<ItemTemplate>
<asp:CheckBox ID="Checkbox1"
checked='<%#CheckYesNo(DataBinder.Eval(Container.DataItem,
"Checked").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem,
"Chktext")%>' Runat=server>
</asp:CheckBox>
<asp:Label visible=False Runat=server ID="Label2"
Text='<%#DataBinder.Eval(Container.DataItem, "Chkvalue")%>' >
</asp:Label>
</ItemTemplate>
</asp:DataList>
*****************************************
Please notice the checked='<%#CheckYesNo(DataBinder.Eval(Container.DataItem,
"Checked").ToString())%>'

this function would look like

protected bool CheckYesNo(string val)
{
if(val=="1")
return(true);

return(false);
}

*****************************************

Value/ID that you do not want to display , but still store with checkbox

<asp:Label visible=False Runat=server ID="Label1"
Text='<%#DataBinder.Eval(Container.DataItem, "Chkvalue")%>' > </asp:Label>

*****************************************


hope this helps
 
P

Patrick Olurotimi Ige

Sreejith thx for the hint..
Using DataList was a good workaround but after thinking over the weekend
i came out with a solution using
FindByValue Method of the LisItem Collection which searches the
collection for a ListItem with a Value property that contains the
specified value and what i did is if it finds the value i set the
checkbox to TRUE.But if it doesn't it returns NOTHING..
See the Msdn documentation here:-
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/h
tml/frlrfSystemWebUIWebControlsListItemCollectionClassFindByValueTopic.a
sp
Patrick
 
Joined
Oct 30, 2006
Messages
1
Reaction score
0
Displaying the value of an item in a table in a checkbox (in a datagrid)

I have a datagrid with at least 6 columns. The last column contains an item template with has 5 checkboxes labeled 0,1, 2, 3, 4 . These checkboxes are referred to the level of difficulty of a certain course depending on the value of that item in the database. The name of that item in a database is "Level". I have tried to get it to work but i wasnt able to.
HELP! :) :loo:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top