2
<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Cartridges_ItemCommand">
    <ItemTemplate>
        <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
        <hr class="hr4" /> 
        <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
        <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton></div>
   </ItemTemplate>
</asp:Repeater>

How can I pass the textbox value within CommandArgument? Sorry totally lost...

ComfortablyNumb
  • 1,448
  • 10
  • 37
  • 64

3 Answers3

3

Did you try: CommandArgument='<%#Eval("cartID") %>'

this is different from yours as it is surrounded by a single quote, I guess this is the correct syntax.

Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47
2

Use FindControl to get other items in the repeater Item. For Example:

 protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
 {
      LinkButton lb = (LinkButton)e.CommandSource;
      string textBoxValue = ((TextBox)lb.Parent.FindControl("cartQty")).Text;         
 }
Wes Grant
  • 2,044
  • 16
  • 14
  • @ComfortablyNumb: you asked for `How can I pass the textbox value within CommandArgument?` and this is not an answer for this specific question. but anyway, good point for you @WesGrant :) – Mohammed Swillam Jun 21 '11 at 16:26
0

you need to bind the cartId to the linkbutton onItemDataBound and then access it onItemCommand, I have modified code for you, give this a go

    <asp:Repeater ID="Cartridges" runat="server" onitemcommand="Repeater_OnItemCommand" OnItemDataBound="Repeater_OnItemDataBound">
<ItemTemplate>
    <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
    <hr class="hr4" /> 
    <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
    <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" Text="Buy"></asp:LinkButton></div>

your onItemdatabound should look like this

 protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

        //your code...

          LinkButton add = (LinkButton)e.Item.FindControl("buy");
                  add.CommandArgument = cartID.ToString();

    }

and then you can access the text box on item command like this

 protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddtoCart")
            {
                LinkButton btnEdit = (LinkButton)e.CommandSource;
                if (btnEdit != null)
                {
                    string editId = btnEdit.CommandArgument;
                    string text = ((TextBox)e.Item.FindControl("cartQty")).Text;
                    //do some stuff with your cartid and quantity
                }
            }
}

You can also extend your code with edit/delete command arguments by adding more linkbuttons and binding them to the correct command and then accessing them in on item command Thanks

Naveed Ahmad
  • 445
  • 3
  • 6