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
Results.aspx
<p> <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox> <asp:Button ID="SearchBtn" runat="server" Text="Button" /> </p> <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" /> <ItemTemplate> Brand: <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /> <br /> Shade: <asp:Label ID="ProductDescLabel" runat="server" Text='<%# Eval("ProductDesc") %>' /> <br /> Price: £<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> <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 /> </ItemTemplate> <SeparatorStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" /> </asp:DataList> </div> <asp:SqlDataSource ID="DSResults" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Product]"></asp:SqlDataSource> </asp:Content>
Results.aspx.vb
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 Else For Each basketItem In BasketList If basketItem.ProductID = item.ProductID Then BasketList.Remove(basketItem) Exit For End If Next End If BasketList.Add(item) Session("BasketList") = BasketList End If Response.Redirect("Basket.aspx") 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