locked
Add To Cart with repeater RRS feed

  • Question

  • User2028673676 posted

    I am trying to create an add to cart in my website.But when I click the button , and when  I go on my addtocart website it show me this

    http://imgur.com/a/Wqtql

    Its not displaying any value at all, any help ? 

        Private Sub btnAddToCart_Click(sender As Object, e As EventArgs) Handles btnAddToCart.Click
    
            Dim ds As DataSet = Nothing
            If Session("sCart") Is Nothing Then
                ds = New DataSet()
                Dim dt As New DataTable()
                dt.Columns.Add(New DataColumn("RoomName"))
                dt.Columns.Add(New DataColumn("RoomPrice", GetType(System.Int32)))
                ds.Tables.Add(dt)
                Session("sCart") = ds
            Else
                ds = DirectCast(Session("sCart"), DataSet)
            End If
            Dim row As DataRow = ds.Tables(0).NewRow()
            row("RoomName") = DirectCast(rptrProductDetails.Controls(0).FindControl("lblRoomName"), Label).Text
            row("RoomPrice") = DirectCast(rptrProductDetails.Controls(0).FindControl("lblRoomPrice"), Label).Text
    
            ds.Tables(0).Rows.Add(row)
    
    
        End Sub

                        <asp:Repeater ID="rptrProductDetails" runat="server">
                            <ItemTemplate>
                                <asp:Label ID="lblRoomPrice" runat="server" Text=""><%#Eval("RoomPrice")%></asp:Label>
                                <asp:Label ID="lblRoomName" runat="server" Text=""><%#Eval("RoomName")%></asp:Label>
                                <h4 class="pull-right"><%#Eval("RoomPrice")%></h4>
                                <h4><%#Eval("RoomName")%>
                                </h4>
                                <p><%#Eval("RoomDesc")%></p>
                            </ItemTemplate>
                        </asp:Repeater>

    Monday, May 15, 2017 12:02 PM

Answers

  • User-707554951 posted

    Hi Dreyar,

    According to your description, I find that the text value in your 'lblRoomName' was always bound to empty. You could bound the data into text filed like this:

    <asp:Label ID="lblRoomPrice" runat="server" Text='<%#Eval("RoomPrice")%>'></asp:Label>

    I made a complete demo, you could refer to it:

    .Aspx:

    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="rptrProductDetails" runat="server">
                <ItemTemplate>
                    <asp:Label ID="lblRoomPrice" runat="server" Text='<%#Eval("RoomPrice")%>'></asp:Label>
                    <asp:Label ID="lblRoomName" runat="server" Text='<%#Eval("RoomName")%>'></asp:Label>
                    <h4 class="pull-right"><%#Eval("RoomPrice")%></h4>
                    <h4><%#Eval("RoomName")%></h4>
                    <asp:Button ID="btnAddToCart" runat="server" Text="AddtoCart" OnClick="btnAddToCart_Click" />
                    <br /><br />
                </ItemTemplate>
            </asp:Repeater>       
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
        </div>
        </form>

    .Aspx.cs:

    Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
    Dim dt As New DataTable()
    dt.Columns.Add(New DataColumn("RoomName"))
    dt.Columns.Add(New DataColumn("RoomPrice", GetType(System.Int32)))
    dt.Rows.Add("name1", 1)
    dt.Rows.Add("name2", 2)
    dt.Rows.Add("name3", 3)
    rptrProductDetails.DataSource = dt
    rptrProductDetails.DataBind()
    End If
    End Sub
    Protected Sub btnAddToCart_Click(sender As Object, e As EventArgs)
    Dim roomName = DirectCast(DirectCast(sender, Button).Parent.FindControl("lblRoomName"), Label)
    Dim roomPrice = DirectCast(DirectCast(sender, Button).Parent.FindControl("lblRoomPrice"), Label)
    Dim ds As DataSet = Nothing
    If Session("sCart") Is Nothing Then
    ds = New DataSet()
    Dim dt As New DataTable()
    dt.Columns.Add(New DataColumn("RoomName"))
    dt.Columns.Add(New DataColumn("RoomPrice", GetType(System.Int32)))
    ds.Tables.Add(dt)
    Session("sCart") = ds
    Else
    ds = DirectCast(Session("sCart"), DataSet)
    End If
    Dim row As DataRow = ds.Tables(0).NewRow()
    row("RoomName") = roomName.Text
    row("RoomPrice") = roomPrice.Text
    'row["RoomName"] = ((Label)rptrProductDetails.Controls[0].FindControl("lblRoomName")).Text;
    'row["RoomPrice"] = ((Label)rptrProductDetails.Controls[0].FindControl("lblRoomPrice")).Text;
    ds.Tables(0).Rows.Add(row)
    GridView1.DataSource = ds.Tables(0)
    GridView1.DataBind()
    End Sub

    Output screenshot as below:

    Best regards,

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 16, 2017 7:51 AM