Form Containing a Repeater that Generates Textboxes and Buttons

B

bob garbados

I need to create a page that displays all of the products from a table and
allows for add to cart functionality.

My thoughts were to display all of the products in table rows using a
repeater. Each row has a text box for quantity to order and a button to add
the product and quantity to the shopping cart. I can dynamically assign the
CommandArgument to my button and figure out which one was clicked, but I
can't figure out how to get the value from the textbox. I am getting errors
when I try to dynamically name the textbox. Any input is appreciated.

Here's the html...
<asp:repeater id="rptProducts" runat="server">
<ItemTemplate>
<tr>
<td class="ListContentRow" valign="top">
<img src="images\<%# DataBinder.Eval(Container.DataItem,
"ProductThumbnail") %>"/>
</td>
<td class="ListContentRow" valign="top">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<a href="">
<font class="BoldLabel">
<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
</font>
</a>
</td>
</tr>
<tr>
<td valign="top">
<font class="ListText">
<%# DataBinder.Eval(Container.DataItem, "ProductBriefDesc")
%><br/>
</font>
</td>
</tr>
</table>
</td>
<td class="ListContentRow" valign="top">
<font class="BoldLabel">
<%# DataBinder.Eval(Container.DataItem, "ProductPrice",
"{0:C}") %>
</font>
</td>
<td class="ListContentRow">
<table cellpadding="4" cellspacing="0">
<tr>
<td valign="top">
<font class="BoldLabel">Qty:&nbsp;</font>
<asp:textbox Columns="3" runat="server"/>
</td>
</tr>
<tr>
<td valign="top">
<asp:button
Text="add to cart"
CssClass="AddToCartButton"
OnCommand="btnAddToCart_Command"
CommandArgument='<%# DataBinder.Eval(Container.DataItem,
"ProductId") %>'
runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
</asp:repeater>
 
K

Karl Seguin

The right thing to do is to assign an OnItemCommand to your repeater and use
that as your event instead of the invidiual button:

<asp:repeater onItemCommand="rptProducts_ItemCommand" >
<itemTemplate>
<asp:button id="btn" runat="server" CommandName="update" />
</itemTemplate>


then in that function, you can do:

TextBox txt = (TextBox)e.Item.FindControl("YourTextBoxId")
if (txt != null){
//do your thing
}


when you raise an event this way, e is the specific ItemTemplate (row if you
will) that actually raised the event. Therefore, doing an
e.Item.FindControl on it will return the specific textbox for that row (even
though they appear to allhave the same id).

Alternatively, use datalist to achieve the same thing...

Karl
 
B

bob garbados

Thanks Karl,

My OnItemCommand event isn't firing...

html:
<asp:repeater OnItemCommand="rptProducts_ItemCommand" id="rptProducts"
runat="server">
....
<asp:textbox ID="txtProductQuantity" Columns="3" runat="server"/>
<asp:button
id="btnAddToCart"
Text="add to cart"
CssClass="AddToCartButton"
CommandName="add"
CommandArgument='<%# DataBinder.Eval(Container.DataItem,
"ProductId") %>'
runat="server" />
....
</repeater>

event handler:
Sub rptProducts_ItemCommand(s as Object, e as RepeaterCommandEventArgs)
lblDebug.Text = "handle event"
End Sub

The page refreshes and the lblDebug label doesn't change. I put a
breakpoint on the label's text assignment in the debugger and it doesn't
break.

Am I missing something simple here? Ideas?
 
K

Karl Seguin

The event won't fire if you are rebinding your repeater on postback ('cuz
the row which fired the event was wiped out (even though it was recreated,
it's different)).

Wrap your binding code in an if not page.ispostback then


Karl
 
B

bob garbados

Fixed. Thanks again for the help Karl. I'm new to asp.net and it's great
to have the newsgroup community to help me get up and running when I'm
stuck.
 

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

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top