0 Replies - 789 Views - Last Post: 15 April 2017 - 08:49 AM Rate Topic: -----

#1 amyc92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-April 17

Search button and textbox for searching within a DataList

Posted 15 April 2017 - 08:49 AM

Pretty new to asp.net vb. I have created a datalist that shows all products in my SQL product table. I then want to use the search button to search this datalist by what is in the text box. Any advice is appreciated.
Thanks in advance

                <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
                <asp:Button ID="SearchBtn" runat="server" Text="Button" />
         <asp:DataList ID="ResultsDL" runat="server" DataKeyField="ProductID" DataSourceID="DSResults" CellPadding="50" Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Overline="False" Font-Size="X-Large" Font-Strikeout="False" Font-Underline="False">
        <AlternatingItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' />
        <br />
        <asp:Label ID="ProductDescLabel" runat="server" Text='<%# Eval("ProductDesc") %>' />
        <br />
        Price:&nbsp; <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' />
        <br />
        <br />
        <asp:Image ID="productimg" runat="server" Height="167px" ImageUrl='<%# Eval("ImagePath", "~/Images/{0}")%>' Width="180px" />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Quantity"></asp:Label>
        &nbsp;<asp:TextBox ID="txtQuantity" runat="server" Width="70px"></asp:TextBox>
        <br />
        <asp:Button ID="selectbtn" runat="server" CommandArgument='<%# Eval("ProductID") %>' CommandName="SelectProduct" Text="Add to basket" Width="180px" style="margin-left: 0px" />
<br />
        <SeparatorStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<asp:SqlDataSource ID="DSResults" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Product]"></asp:SqlDataSource>

Partial Class Results
    Inherits System.Web.UI.Page
    Dim BasketList As New List(Of Item)

    Protected Sub ResultsDL_ItemCommand(source As Object, e As DataListCommandEventArgs) Handles ResultsDL.ItemCommand
        If (e.CommandName = "SelectProduct") Then
            Session("ProductID") = CInt(e.CommandArgument)

            Dim txtQuantity As TextBox = TryCast(ResultsDL.Items(e.Item.ItemIndex).FindControl("txtQuantity"), TextBox)

            If IsNumeric(txtQuantity.Text) Then 'don't proceed if quantity is not a number
                Dim item As New Item()
                item.SessionID = Session("SessionID")
                item.ProductID = Session("ProductID")
                item.ProductName = TryCast(ResultsDL.Items(e.Item.ItemIndex).FindControl("ProductNameLabel"), Label).Text
                item.Quantity = CInt(txtQuantity.Text)
                item.UnitPrice = CDbl(TryCast(ResultsDL.Items(e.Item.ItemIndex).FindControl("PriceLabel"), Label).Text)
                item.Amount = item.UnitPrice * item.Quantity

                If IsNothing(BasketList) Then 'if basket doesn't exist
                    BasketList = New List(Of Item) 'create new basket
                    For Each basketItem In BasketList
                        If basketItem.ProductID = item.ProductID Then
                            Exit For
                        End If
                End If

                Session("BasketList") = BasketList
            End If

            End If
    End Sub

    Private Sub Results_Load(sender As Object, e As EventArgs) Handles Me.Load
        BasketList = TryCast(Session("BasketList"), List(Of Item))
    End Sub
End Class

Is This A Good Question/Topic? 0
  • +

Page 1 of 1