Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing checkboxes in a repeater control

Status
Not open for further replies.

JanduBalm

Programmer
Jun 8, 2005
3
GB
Hi,

I have a repeater which displays lists of items. For each item, a checkbox is displayed and is named using the prefix of chk and then the product id is appended.

The code is as follows:

Code:
<ItemTemplate>
	<tr>
		<td><%#Container.DataItem("ImageLink")%></td>
		<td><%#Container.DataItem("Name")%></td>
		<td><%#Container.DataItem("Description")%></td>
		<td><input type="checkbox" id="chk<%#Container.DataItem("ProductID")%>"></td>
	</tr>
	<tr>
		<td colspan="4">
			<hr noshade>
		</td>
	</tr>
</ItemTemplate>

The user can select as many items as he/she wants from this list and can then add them to a shopping cart.

My question is:
1) How can I access the checkboxes in the repeater control.

I am using VB.NET. Any help will be very much appreciated. I need to get this working for a system demo tomorrow.
 
with this method you will not be able to access the check box in code behind because you are using a traditional HTML DOM element, not a WebControl. Use a CheckBox webcontrol if you want to access this from the code behind.

once in the code behind you need to iterate over repeaters items. find the checkbox control from the id and get the value. it can have a value with a CheckBox control. There are alot of examples of this online.

that said you should change the ID of the check box to simply ProductIdCheckBox or something. this way you can located the control. if checked get the value from the checkbox.

if you need to access this client side, then that is better suited for forum216.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi jmeckley,

Many thanks for your guidance on this one

As per your suggestion, I updated the code to the following:

Code:
<ItemTemplate>
	<tr>
		<td><%#Container.DataItem("ImageLink")%></td>
		<td><%#Container.DataItem("Name")%></td>
		<td><%#Container.DataItem("Description")%></td>
		<td><asp:checkbox id="chkProduct" value="<%#Container.DataItem("ProductID")%>" runat="server"/></td>
	</tr>
	<tr>
		<td colspan="4"><hr noshade></td>
	</tr>
</ItemTemplate>

When I built the project and viewed the page in the browser the checkboxes are not rendered. And if I view source, it seems that the browser is treating it as text and not a tag.

Any thoughts?
 
There is no value property for an asp.net checkbox change it to text:
Change this
Code:
<asp:checkbox id="chkProduct" [red]value[/red]="<%#Container.DataItem("ProductID")%>" runat="server"/>
to this
Code:
<asp:checkbox id="chkProduct" [red]text[/red]="<%#Container.DataItem("ProductID")%>" runat="server"/>
 
Hi,

If anyone is interested this is how I have resolved my problem:

.aspx file
Code:
<ItemTemplate>
	<tr>
		<td><%#Container.DataItem("ImageLink")%></td>
		<td><%#Container.DataItem("Name")%></td>
		<td><%#Container.DataItem("Description")%></td>
		<td class="HideText"><asp:checkbox id="chkSelectProduct"  text='<%#Container.DataItem("ProductID")%>' runat="server"/></td>
	</tr>
	<tr>
		<td colspan="4">
	 		<hr noshade>
		</td>
	</tr>
</ItemTemplate>

NOTE: Single quotes are needed around the text element to ensure that the checkbox is rendered correctly on the page. This drove me crazy for an entire day while I tried to work out why it was not working.

.aspx.vb file
Code:
lblErrorMessage.Text = ""
Dim l_intRepeaterItem As RepeaterItem

Try
	For Each l_intRepeaterItem In rptProducts.Items
        	If l_intRepeaterItem.ItemType = ListItemType.Item Or l_intRepeaterItem.ItemType = ListItemType.AlternatingItem Then
	   		Dim chkProductHasBeenSelected As CheckBox = CType(l_intRepeaterItem.FindControl("chkSelectProduct"), CheckBox)

			
	                If chkProductHasBeenSelected.Checked = True Then
        	        	g_strSQL = "INSERT INTO tblOrderProducts (ProductID) VALUES (" & chkProductHasBeenSelected.Text & ")"
	                        lblErrorMessage.Text += g_strSQL & "<br>"
	               	End If

                End If
	Next

Catch ex As Exception
	lblErrorMessage.Text = ex.Source & "<br>" & ex.Message & "<br>" & ex.StackTrace
End Try

Many thanks for the help :)

 
You need single quotes around the text property in your case because you have double quotes around the column name. Otherwise, just using double quotes would be fine.
 
I don't mean to hijack this post, but i'm working on a similar task and I want to add to this this slightly. I have a table in a repeater like yours with the checkboxes in the first column. The remaining table columns have data relavent to the item:
Code:
<ItemTemplate>
<tr style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri, Tahoma, Verdana">
	<td><asp:CheckBox id='cbCall' Text='<%#Container.DataItem("id")%>' checked='<%#Container.DataItem("CheckMe")%>' AutoPostBack=False runat='server'>
							</asp:CheckBox></td>
	<td><%#Container.DataItem("date")%></td>
	<td><%#Container.DataItem("time")%></td>
	<td><%#Container.DataItem("CallTo")%></td>
</tr>
</ItemTemplate>

What I want to do is NOT display the id data field in the text, But, I need it when updating the checked records. How can I hide that value, but still have it available when looping through and evaluating the checkboxes?

mwa
<><
 
use a HiddenField WebControl

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Ahh... I got it. In the loop, find the checkbox and determine if the checkbox is checked, if so then also go find the hidden id associated with that item.

Piece of cake...

Thanks!

mwa
<><
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top