why won't this code work???

S

Stimp

I have a datalist with a checkbox field.. when a button is pressed, I
want to output which checkboxes are selected.

<asp:datalist id="DataList1" runat="server">
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" border="0" width="100%">
<TR>
<TD>
<asp:CheckBox id="chkVehicleID" runat="server"></asp:CheckBox></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:datalist>

<asp:Button id="Button8" runat="server"></asp:Button>

Sub Button8_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button8.Click

Dim dlItem As DataListItem

For Each dlItem In DataList1.Items

Response.Write(
CType(dlItem.FindControl("chkVehicleID"), CheckBox).Checked)
Next

End Sub

When I check a few boxes and press the button, I just get a list of
FALSE values... what's wrong here?!?

Thanks,
Peter
 
P

Patrick.O.Ige

For Each dlItem In DataList1.Items
chkVehicleID = dlItem.FindControl("chkVehicleID")
If chkVehicleID.Checked Then
'Get your selected values here
End If
Next
Hope that helps
Patrick
 
S

Stimp

For Each dlItem In DataList1.Items
chkVehicleID = dlItem.FindControl("chkVehicleID")
If chkVehicleID.Checked Then
'Get your selected values here
End If
Next
Hope that helps
Patrick

cheers.. it turns out my original code was fine, but that I was
refreshing the datalist each time my page loaded

i.e. I hadn't placed a 'If Not IsPostBack' statement before the datalist
output.

so it was overwriting the state of my datalist. Doh!
 

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

Top