473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Form Containing a Repeater that Generates Textboxes and Buttons

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>
Nov 18 '05 #1
4 1698
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"bob garbados" <bo*********@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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>

Nov 18 '05 #2
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?
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OO**************@TK2MSFTNGP09.phx.gbl...
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"bob garbados" <bo*********@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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>


Nov 18 '05 #3
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"bob garbados" <bo*********@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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?
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OO**************@TK2MSFTNGP09.phx.gbl...
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"bob garbados" <bo*********@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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>



Nov 18 '05 #4
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.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uR**************@TK2MSFTNGP10.phx.gbl...
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"bob garbados" <bo*********@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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?
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OO**************@TK2MSFTNGP09.phx.gbl...
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"bob garbados" <bo*********@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
> 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>
>
>



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Kirk | last post by:
Hi are there any "gotchas" with using an asp:repeater that means that the "onclick" method of a LinkButton created in the repaeter does not fire? I at least cannot get it to work. I have a...
4
by: MattB | last post by:
This is just a rephrased version of a question I posted earlier. I think I'm closer now, so it seemed worthy of a new (more specific) post. In my repeater I'm dynamically creating text boxes, so...
6
by: Sean | last post by:
HI There, I am making the transition from asp to asp .net, I am currenty writing an application that requires a bulk insert from a webform into SQL server, normally I would just create rows of...
2
by: Big E | last post by:
I'm using ASP.Net and SQL Server. I have a table called states. I have 2 forms. On form 1 is the 50 states in textboxes created with a loop. I want to have those 50 or 45 or whatever amount of...
11
by: Doug Bell | last post by:
Hi, I am trying to create form that displays data like an Access continuous dataform rather than using a data grid. I am thinking that I can achieve this by creating a row of text boxes, one...
20
by: Robert | last post by:
Need some help to stop me going around in circles on this one.... Have a nested subform (subform2) which simulates a continuous form for the record on the parent subform. Subform2 has rows of...
1
by: Timbo | last post by:
Hi all, This is my first message here so i'll try and include all the information that will help you help me out, if possible. Basically I am using C# in ASP.NET 2.0 and have a Repeater...
0
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
(This is a continuation of my earlier post - 'Beginners question about binding/formatting a repeater'.) I'm trying to write a fairly minimal web app using a repeater control. I am accustomed to...
4
by: zufie | last post by:
I have a main form containing a command SEND button that prompts an email form to pop up. The email address(es) that are supposed to appear on the email form are those corresponding to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.