Form Containing a Repeater that Generates Textboxes and Buttons

Discussion in 'ASP .Net' started by bob garbados, Oct 5, 2004.

  1. bob garbados

    bob garbados Guest

    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>
     
    bob garbados, Oct 5, 2004
    #1
    1. Advertising

  2. bob garbados

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > 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>
    >
    >
     
    Karl Seguin, Oct 5, 2004
    #2
    1. Advertising

  3. bob garbados

    bob garbados Guest

    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:...
    > 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" <> wrote in message
    > news:...
    > > 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>
    > >
    > >

    >
    >
     
    bob garbados, Oct 5, 2004
    #3
  4. bob garbados

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > 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:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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>
    > > >
    > > >

    > >
    > >

    >
    >
    >
     
    Karl Seguin, Oct 5, 2004
    #4
  5. bob garbados

    bob garbados Guest

    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:...
    > 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" <> wrote in message
    > news:...
    > > 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:...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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>
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >
    > >

    >
    >
     
    bob garbados, Oct 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bivin
    Replies:
    0
    Views:
    487
    bivin
    Jul 8, 2003
  2. Neo
    Replies:
    0
    Views:
    441
  3. Timbo
    Replies:
    1
    Views:
    5,971
    bruce barker \(sqlwork.com\)
    May 5, 2006
  4. Dan Sikorsky
    Replies:
    2
    Views:
    531
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 3, 2007
  5. Davido
    Replies:
    3
    Views:
    809
    Bergamot
    Jan 2, 2007
Loading...

Share This Page