473,897 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.It ems.Count - 1
cb = CType(Me.DataLi st1.Items(i).Co ntrols(7), CheckBox)
If cb.Checked = True Then ' THIS ALWAYS SAYS FALSE!!!
' get the job id and the job title
MyJobId = CType(Me.DataLi st1.Items(i).Co ntrols(1), Label)
MyJob = CType(Me.DataLi st1.Items(i).Co ntrols(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 3787
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.DataI tem, "MyJobId")
%>
</asp:label>
<asp:label id="lblMyJob" runat="server">
<%# DataBinder.Eval ( Container.DataI tem, "MyJob") %>
</asp:label>
<asp:label id="Label3" runat="server">
<%# DataBinder.Eval (Container.Data Item,
"CurrencyValue" ) %>
</asp:label>
<asp:CheckBox id="thecheckbox " runat="server"
Checked='<%# DataBinder.Eval (Container.Data Item, "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.EventArg s) Handles MyBase.Load
If Not IsPostBack Then
DataList1.DataS ource = CreateDataSourc e()
DataList1.DataB ind()
End If
End Sub
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
Dim i As Integer, cb As CheckBox, _
MyJobId As DataBoundLitera lControl, _
MyJob As DataBoundLitera lControl, _
JobAppliedFor As String
For i = 0 To Me.DataList1.It ems.Count - 1
cb = DataList1.Items (i).FindControl ("thecheckbo x")
If Not IsNothing(cb) Then
If cb.Checked = True Then
MyJobId = DataList1.Items (i). _
FindControl("lb lMyJobID").Cont rols(0)
MyJob = DataList1.Items (i). _
FindControl("lb lMyJob").Contro ls(0)
JobAppliedFor = JobAppliedFor & MyJobId.Text & ":" & _
MyJob.Text & ","
End If
End If
Next
Response.Write( JobAppliedFor)
End Sub
Function CreateDataSourc e() 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 'CreateDataSour ce

"VB Programmer" <xD**********@D ont.com> wrote in message
news:u3******** ******@tk2msftn gp13.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.It ems.Count - 1
cb = CType(Me.DataLi st1.Items(i).Co ntrols(7), CheckBox)
If cb.Checked = True Then ' THIS ALWAYS SAYS FALSE!!!
' get the job id and the job title
MyJobId = CType(Me.DataLi st1.Items(i).Co ntrols(1), Label)
MyJob = CType(Me.DataLi st1.Items(i).Co ntrols(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
2466
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 underneath. Now, the rub here is that every checkbox on the page (except the "All"s)
3
1521
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 load I am setting the values of my checkboxes: chk1.Attributes.Add("checkValue", "151010") chk2.Attributes.Add("checkValue", "151020") chk3.Attributes.Add("checkValue", "151030") chk4.Attributes.Add("checkValue", "151040")...
1
1359
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 status of the checkbox whether it is checked or not?\ Thank you
0
1292
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 type="hidden" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "I_QUESTION_ID") %>' ></input>
0
1998
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 control. I have a column in my database that hold the current status of the items in there. If I used a checkboxlist and populate it with my options, and set the SQL statement on my gridview control to: "SELECT , , , FROM WHERE
1
2179
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 Labels existing in the DataList should be visible but if the CheckBox is unchecked by the user, the Labels inside the DataList should be made invisible. How do I do this? Note that the AutoPostBack property of the CheckBox has been set to
0
1183
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 foreach (DataListItem dli in DataList1.Items) { bool ok = ((CheckBox)dli.FindControl("CheckBox1")).Checked;
5
5818
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 foreach (DataListItem dli in DataList1.Items) { bool ok = ((CheckBox)dli.FindControl("CheckBox1")).Checked;
4
1172
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 foreach (DataListItem dli in DataList1.Items) { bool ok = ((CheckBox)dli.FindControl("CheckBox1")).Checked;
0
9990
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
11250
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10850
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10941
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4698
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4293
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.