472,353 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 3696
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...
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...
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...
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...
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.