Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8260
  • Last Modified:

ASP Repeater - Shopping cart quantity problem

My question is as follows:

The repeater what attached here it works well. The only problem is that I need to identify what is the “Quantity” value in each row.

When I need to identify a value from the repeater (SqlDataReader) I use the “<%#DataBinder.Eval(Container.DataItem, "SomeField")%>“,

But which syntax I could use to identify a field like “quantity” what is not a part of the SqlDataReader?

Attention please: <a id="btnAdd" href=”UpdateCart.aspx?ProductID=<%#DataBinder.Eval(Container.DataItem, "ProductID")%>”

The source ASP code as follows:

***Begin***

<asp:repeater id="Items" Runat="server">
            <ItemTemplate>
                        <tr bgcolor="#eeeeee">
                                    <td class="Listing"><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
                                                <td class="Listing"><strong><%#DataBinder.Eval
(Container.DataItem, "DayPrice","{0:C2}")%></strong></td>
                                                            <td class="Listing"><font color="#666666"><%#DataBinder.Eval(Container.DataItem, "WeekPrice","{0:C2}")%></font></td>
                                                                       <td>
                                                                                    <div align="center">
                                                                                                <asp:textbox ID="Quantity" size="3" Runat="server">1</asp:textbox>
                                                                                    </div>
                                                                        </td>
                                                                        <td>
<div align="right"><asp:imageButton id="AddToCart" runet="server" OnClick="<%# AddToMyCart(DataBinder.Eval(Container.DataItem, "ProductID"),DataBinder.Eval(Container.DataItem, "ShoppingCartID"),DataBinder.Eval(Container.DataItem, "Quantity"))"
/></div></td></tr>
            </ItemTemplate>
</asp:repeater>
***End***

Now, I have in the VB code behind a sub what his job is to append it to the shopping cart, and it also works well.

So again, how i get access to the current "Quantity" TextBox?

 

Thanks for your time.

0
isaacab
Asked:
isaacab
  • 8
  • 5
  • 2
1 Solution
 
vladimir_kalashnikovCommented:
you can just put an inline call in the text box:

 <asp:textbox ID="Quantity" size="3" Runat="server"><%# GetQuantity(value) </asp:textbox>

this calls the function "GetQuantity" which you can define in your code behind.  just pass in whatever value you need to put in the quantity.
0
 
isaacabAuthor Commented:
Thanks for your time,

My question was, How to "SET" from a repeater to a DB table, but not how to "GET".
Any ideas?
0
 
vladimir_kalashnikovCommented:
oh, i see.  You could just put a text box in the repeater and have a link that takes the value from the text box and updates the page.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
isaacabAuthor Commented:

Thanks,

This exactly is my question, How to take the value from the "current" repeat line? How to take the "current" Product ID i know (<%#DataBinder.Eval(Container.DataItem, "ProductID")%>), But is there a way to take the "current" quantity value from my text box what is NOT a part of the Container.DataItem?

Please help. thanks again.
0
 
mmarinovCommented:
HI isaacab,

for the your question the answer - you can not do this directly. This is because Container.DataItem is connected with the current datasource property of the reperater control. You can easily to use the ItemDataBound event of the reperater control and there based of the productId value to get the quantity from another query.
Another way of make this working is to modify your sql query to return and quanity in the same repeater.

If you have any problems with programing of one of the scenarious above, please let me know

Regards
mmarinov
0
 
isaacabAuthor Commented:
Thanks mmarinov,
My problem is not how to GET the quantity value from my DataSource. that's i know. The problem is, how to UPDATE the quantity value in my DataSource when the quantity text-box is NOT a part of the dataSource. That's mean, when a customer will change the quantity and he will clicks on the "Add to cart" button, How i take the new quantity value what the customer put in, and put it in my dataSource table?
Please help. Thanks again.
0
 
mmarinovCommented:
HI isaacab,

I think i understand your question now. So i suggeest you to use ItemCommand event of the Repeater control and add to your ImageButton control CommandName="AddToCart".
So the code can be something like this:
<asp:repeater id="Items" Runat="server" OnItemCommand="Items_ItemCommand">
            <ItemTemplate>
                        <tr bgcolor="#eeeeee">
                                    <td class="Listing"><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
                                                <td class="Listing"><strong><%#DataBinder.Eval
(Container.DataItem, "DayPrice","{0:C2}")%></strong></td>
                                                            <td class="Listing"><font color="#666666"><%#DataBinder.Eval(Container.DataItem, "WeekPrice","{0:C2}")%></font></td>
                                                                       <td>
                                                                                    <div align="center">
                                                                                                <asp:textbox ID="Quantity" size="3" Runat="server">1</asp:textbox>
                                                                                    </div>
                                                                        </td>
                                                                        <td>
<div align="right"><asp:imageButton id="AddToCart" runet="server" CommandName="AddToCart"/></div></td></tr>
            </ItemTemplate>
</asp:repeater>

c#
------------------
void Items_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{        
 if(e.CommandName == "AddToCart")
  {
    TextBox quantityTextBox = (TextBox)e.Item.FindControl("Quantity");
    //to get the content of the new quantity you can use quantityTextBox.Text
    //and call your method AddToMyCart
  }
}

vb.net
-------------------
Sub Items_ItemCommand(Sender As Object, e As RepeaterCommandEventArgs)
       if e.CommandName = "AddToCart" Then
           Dim quantityTextBox as TextBox = CType(e.Item.FindControl("Quantity"), TextBox)
           'to get the content of the new quantity you can use quantityTextBox.Text
           'and call your method AddToMyCart
       End If
End Sub


Hope This Helps
Regards
mmarinov
0
 
isaacabAuthor Commented:
Thanks a lot mmarinov for your time,

This actually works fine, but...
Your solution works only if i have a ADD TO CART button on every item in my repeater, but i have only 1 ADD TO CART button in the bottom of my page. So, when the user will clicks on the ADD TO CART button, i need to loop thru all the items in my repeater, and i cannot do it, because i cannot send the "RepeaterCommandEventArgs" from the ImageButton.

Do you have a solution for this?

Please help. Thanks in advance.
0
 
mmarinovCommented:
Hi isaacab,

Yes this is true. The code is working for add to cart button on every item in the repeater.
For your scenario this should work:

vb.net
----------------------
Sub AddToCart_Click(Sender As Object, e As EventArgs)
       
        Dim item As RepeaterItem
        For Each item In  Items.Items
            Label1.Text &= _
                Dim quantityTextBox as TextBox = CType(item.FindControl("Quantity"), TextBox)
        Next item
    End Sub

c#
---------------------
void AddToCart_Click(Object Sender, EventArgs e)
      {        
         foreach(RepeaterItem item in Items.Items)
         {        
            TextBox quantityTextBox = (TextBox)e.Item.FindControl("Quantity");
         }
      }


HTH
mmarinov
0
 
isaacabAuthor Commented:

Thanks a lot Martin, It works now wery well.

You help me a lot. Of course is the 500 points for you.

0
 
mmarinovCommented:
Thanks Isaacab,
Glad to help

Regards
mmarinov
0
 
isaacabAuthor Commented:
Sorry Martin for bother you,
Do you have an idea if i would like to have a button on EVERY item, but instead a ImageButton i want put an HTML anchor.
So, what i have to write instead CommandName="AddToCart"? the HTML don't have the CommandName property.

Thanks in advance.
0
 
mmarinovCommented:
Hi isaacab ,

Why do you want to do that? May be if you post the reason for this requirement - it will be much easier to find a solution.

Regards
mmarinov
0
 
isaacabAuthor Commented:
Thanks for your fast answer Martin,

The reason is, because i would like to use the "OnMouseOver" and "OnMouseOut" events. The example is below:

<a id="AddToCart" href="#" runat="server" CommandName="AddToCart" onMouseOver="MM_swapImage('add1','','images/add_h.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="images/add.gif" name="add1" width="43" height="19" border="0" id="add"></a>

The OnMouse events doing the buttons lively. The JavaScript functions (MM_swapImgRestore() and MM_swapImage()) doing the changes.

So, I use here the CommandName property, but it doesn't working. Do you know why?

Thanks a lot Martin.

0
 
isaacabAuthor Commented:
The Final solution is as follows:

<asp:linkbutton id="AddToCart" runat="server" onMouseOver="MM_swapImage('Add','','images/rental_add_h.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="images/rental_add.gif" name="add1" width="43" height="19" border="0" id="add"></asp:linkbutton>

And the VB code:

        Sub Items_ItemCommand(ByVal Sender As Object, ByVal e As RepeaterCommandEventArgs)

        Dim ItProductID As New TextBox, ItQuantity As New TextBox

        ItProductID = CType(e.Item.FindControl("ItemProductID"), TextBox)
        ItQuantity = CType(e.Item.FindControl("ItemQuantity"), TextBox)
       
        If Not (ItQuantity.Text Is DBNull.Value) Then
            'Here call your AddToCart method
        End If

        End Sub

Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now