473,412 Members | 4,519 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,412 software developers and data experts.

Help: Checkbox in datalist - checked status = ALWAYS FALSE!

Simple: I have a datalist that is holding a bunch of job opportunities.
Beside each opp (in the item template) there's a checkbox that says "apply".

After the datalist there's a button that say's "process". I want to LOOP
through each datalist entry and see if the user checked the check box.
Simple.

I wrote the code, but the checked value is ALWAYS False. Any ideas?

Here's the code...
Dim i As Integer, cb As CheckBox, MyJobId As Label, MyJob As Label,
JobAppliedFor As String
For i = 0 To Me.DataList1.Items.Count - 1
cb = CType(Me.DataList1.Items(i).Controls(7), CheckBox)
If cb.Checked = True Then ' THIS ALWAYS SAYS FALSE!!!
' get the job id and the job title
MyJobId = CType(Me.DataList1.Items(i).Controls(1), Label)
MyJob = CType(Me.DataList1.Items(i).Controls(3), Label)

' build a string of everything they checked
JobAppliedFor += MyJobId.Text & ":" & MyJob.Text & ","
End If
Next
Response.Write(JobAppliedFor)

Thanks in advance!
Nov 19 '05 #1
1 3750
Hi ???

Here's my solution to that one. Your problem is that you're looking for the
wrong thing in the wrong place! You need to find a control inside the
label, believe it or not.

It helps to set trace="true" and look through the hierarchy of the page.

Check out the code below and let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<form id="Form1" method="post" runat="server">
<asp:datalist id="DataList1" runat="server">
<itemtemplate>
<asp:label id="lblMyJobId" runat="server">
<%# DataBinder.Eval( Container.DataItem, "MyJobId")
%>
</asp:label>
<asp:label id="lblMyJob" runat="server">
<%# DataBinder.Eval( Container.DataItem, "MyJob") %>
</asp:label>
<asp:label id="Label3" runat="server">
<%# DataBinder.Eval(Container.DataItem,
"CurrencyValue") %>
</asp:label>
<asp:CheckBox id="thecheckbox" runat="server"
Checked='<%# DataBinder.Eval(Container.DataItem, "Boolean") %>'>
</asp:checkbox>
</itemtemplate>
</asp:datalist>
<p>
<asp:button id="Button1" runat="server"
Text="Button"></asp:button></p>
</form>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataList1.DataSource = CreateDataSource()
DataList1.DataBind()
End If
End Sub
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer, cb As CheckBox, _
MyJobId As DataBoundLiteralControl, _
MyJob As DataBoundLiteralControl, _
JobAppliedFor As String
For i = 0 To Me.DataList1.Items.Count - 1
cb = DataList1.Items(i).FindControl("thecheckbox")
If Not IsNothing(cb) Then
If cb.Checked = True Then
MyJobId = DataList1.Items(i). _
FindControl("lblMyJobID").Controls(0)
MyJob = DataList1.Items(i). _
FindControl("lblMyJob").Controls(0)
JobAppliedFor = JobAppliedFor & MyJobId.Text & ":" & _
MyJob.Text & ","
End If
End If
Next
Response.Write(JobAppliedFor)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("MyJobId", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("MyJob", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Job " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

"VB Programmer" <xD**********@Dont.com> wrote in message
news:u3**************@tk2msftngp13.phx.gbl...
Simple: I have a datalist that is holding a bunch of job opportunities.
Beside each opp (in the item template) there's a checkbox that says
"apply".

After the datalist there's a button that say's "process". I want to LOOP
through each datalist entry and see if the user checked the check box.
Simple.

I wrote the code, but the checked value is ALWAYS False. Any ideas?

Here's the code...
Dim i As Integer, cb As CheckBox, MyJobId As Label, MyJob As Label,
JobAppliedFor As String
For i = 0 To Me.DataList1.Items.Count - 1
cb = CType(Me.DataList1.Items(i).Controls(7), CheckBox)
If cb.Checked = True Then ' THIS ALWAYS SAYS FALSE!!!
' get the job id and the job title
MyJobId = CType(Me.DataList1.Items(i).Controls(1), Label)
MyJob = CType(Me.DataList1.Items(i).Controls(3), Label)

' build a string of everything they checked
JobAppliedFor += MyJobId.Text & ":" & MyJob.Text & ","
End If
Next
Response.Write(JobAppliedFor)

Thanks in advance!


Nov 19 '05 #2

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

Similar topics

3
by: Adam Toline | last post by:
In reference to the following: http://www.bellecose.com/form.htm At the top of each column there is a box for "All". When one is checked I need to check all of (and only) those boxes...
3
by: Amy Snyder | last post by:
For what I thought would be a very simple task is turning out to be very frustrating. I have been trying for a day now to extract checkbox values from my form and nothing is working. On my form...
1
by: Vannela | last post by:
I want to get the status of the checkbox control in the item template of the DataList in the code behind Using findcontrol i am not able to get the status of the checkbox . How shouls i get the...
0
by: yurps | last post by:
Hello I have a DataList like this <asp:DataList id="Questions" Runat="server"> <ItemTemplate> <table border="1"> <tr> <td> <asp:CheckBox ID="Yes" Runat="server"></asp:CheckBox> <input...
0
by: Faybert | last post by:
Hello, and Thanks in advance for any light you might shed on my troubles. I'm trying to setup a series of checkboxes, or a checkboxlist to control the results that are shown on a gridview...
1
by: rn5a | last post by:
A ASPX page has a DataList control with a few Labels. This page also has a CheckBox which resides OUTSIDE the DataList. By default, this CheckBox is checked. If the CheckBox is checked, then the...
0
by: simonakiki | last post by:
i have checkbox in datalist and i need to find which checkbox is checked name of checkbox = CheckBox1 and name of datalist is DataList1 i am writng this code to find the checked checkbox ...
5
by: simonakiki | last post by:
i have checkbox in datalist and i need to find which checkbox is checked name of checkbox = CheckBox1 and name of datalist is DataList1 i am writng this code to find the checked checkbox ...
4
by: simonakiki | last post by:
i have checkbox in datalist and i need to find which checkbox is checked name of checkbox = CheckBox1 and name of datalist is DataList1 i am writng this code to find the checked checkbox ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.