Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Form Containing a Repeater that Generates Textboxes and Buttons

Reply
Thread Tools

Form Containing a Repeater that Generates Textboxes and Buttons

 
 
bob garbados
Guest
Posts: n/a
 
      10-05-2004
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>


 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      10-05-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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>
>
>



 
Reply With Quote
 
 
 
 
bob garbados
Guest
Posts: n/a
 
      10-05-2004
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:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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>
> >
> >

>
>



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      10-05-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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>
> > >
> > >

> >
> >

>
>
>



 
Reply With Quote
 
bob garbados
Guest
Posts: n/a
 
      10-05-2004
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:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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:(E-Mail Removed)...
> > > 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" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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>
> > > >
> > > >
> > >
> > >

> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounded-Corners MouseOver Popups w/Gradients,Buttons,Textboxes,etc Dan Sikorsky ASP .Net 2 02-03-2007 11:10 PM
iframe containing form submit buttons appear not active Davido HTML 3 01-02-2007 09:51 PM
Control inside asp:Repeater generates ArgumentException: Invalid postback or callback argument. Timbo ASP .Net 1 05-05-2006 04:34 PM
About using repeater control with textboxes and label in the client side Neo ASP .Net 0 03-22-2006 09:36 AM
REPEATER with TEXTBOXES(How can I get values in textbox) bivin ASP .Net 0 07-08-2003 10:00 AM



Advertisments