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

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

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!
 
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
 
Back
Top