Collecting Textbox Input from Repeater (Null obj reference)

FastPlymouth

Newcomer
Joined
Jan 26, 2006
Messages
8
Hello All,

I am getting the standard "Object variable or With block variable not set" error on the following code. I have a repeater with some textboxes that I'm trying to collect the input from in an event handler triggered by a button outside the repeater control.

Am I missing a step somewhere?

Code:
    Protected Sub addCart_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' If add to cart button is pressed, check all qty boxes for quantities
        objCartDT = Session("Ordering_Cart")
        Dim i As Integer = 0

        For i = 0 To itemRepeater.Items.Count - 1
            Dim dataItem As RepeaterItem = itemRepeater.Items(i)

            ' Validate non-empty qty boxes
            If IsNumeric(CType(dataItem.FindControl("qty"), TextBox).Text) Then
                Dim itemQty As Integer = CInt(Regex.Replace(CType(dataItem.FindControl("qty"), TextBox).Text, "'", ""))

***SNIP***
            End If
        Next
End Sub
Any ideas?

Thanks!
 
Had a thought last night - could it be that one of the items in the collection is the header template row and therefore no textbox control with id "qty" exists at that point?

Going to experiment with that for a bit.

Thanks!
 
Still a no-go. Here is a snip from my .aspx

Code:
<asp:Repeater id="itemRepeater" Runat="server">
	<HeaderTemplate>
		<table id="wrapper" cellspacing="0" cellpadding="0">
		<tr class="legend">
			<th>Item No</th>
			<th>Remaining Allocation</th>
			<th>Qty</th>
			<th></th>
			<th>Description</th>
			<th>Price</th>
			<th>Item Image</th>
		</tr>
	</HeaderTemplate>
	<ItemTemplate>
		<tr class="product">
			<td><%# DataBinder.Eval(Container.DataItem, "itemNo") %></td>
			<td><%# DataBinder.Eval(Container.DataItem, "Limit") %></td>
			<td><asp:TextBox ID="qty" class="qty" Runat="server"></asp:TextBox></td>
			<td class="cartColumn">
			<%# DataBinder.Eval(Container.DataItem, "itemNew") %>
			</td>
			<td><a href="javascript:n_window('details.aspx?id=<%# DataBinder.Eval(Container.DataItem, "id") %>')" title="Click for Details">
			<%# DataBinder.Eval(Container.DataItem, "shortdesc") %></a><p>Pkg of <%# DataBinder.Eval(Container.DataItem, "units") %></p>
			</td>
			<td><%# FormatCurrency(DataBinder.Eval(Container.DataItem, "price")) %></td>
			<td class="image">
				<a href="javascript:n_window('details.aspx?id=<%# DataBinder.Eval(Container.DataItem, "id") %>')" title="Click to Enlarge">
	        		<%# DataBinder.Eval(Container.DataItem, "Thumb") %></a>
			</td>
		</tr>
	</ItemTemplate>
	<FooterTemplate>
		</table>
	</FooterTemplate>
</asp:Repeater>
<asp:Button ID="addCart" Text="Add to Cart" Runat="server" OnClick="addCart_Click" />
 
The error occurs on this line:

Dim itemID As Integer = dataItem.DataItem("id")

I'm guessing I can't retrieve that information unless the event handler is of the updatecommand type from within the repeater control?
 
OK - I guess my real question is:

Is there a way to pass data (without using the commandArgument or commandName as my button is outside the repeater) from a repeater to an onClick handler? Textboxes do not have those attributes so all I can glean from them is the text from within them. I can get the index of the current row but that doesn't help me identify exactly what the database-inserted id of the current item is.

Any advice would be appreciated.

Thanks!
 
Fixed the problem. I ended up getting hacky and used hidden form fields that grabbed the values I wanted at the time the repeater is populated. In my code-behind click event handler I just referenced those hidden fields' values.

This whole thing is stupid if you ask me - I think the repeater is still memory-resident at the time I'm calling this handler so I don't see why .net can't access the bound columns, but whatever.
 
Back
Top