Search code examples
htmlasp.netvb.net

Search function not working in .aspx file


I have a web page called Narudzbe.aspx that displays a GridView of orders. I'm trying to implement a search functionality where the user can enter a specific order number and retrieve the corresponding order. However, the search function doesn't seem to be working as expected.

Here's the relevant code:

Narudzbe.aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Narudzbe.aspx.vb" Inherits="Mišković_OMIS_projekt.Narudzbe" %>

<!DOCTYPE html>
<!-- Rest of the HTML code -->

<form id="form2" runat="server">
    <!-- Rest of the form code -->
    <div class="search">
        <input type="text" id="txtSearch" placeholder="Pretraži po broju narudžbe"/>
        <input type="button" id="btnSearch" value="Pretraži" onclick="btnSearch_Click"/>
        <!-- Rest of the code -->
    </div>
</form>

Narudzbe.aspx.vb:

Imports System.Data.SqlClient

Partial Class Narudzbe
    Inherits System.Web.UI.Page

    Protected WithEvents btnSearch As Button
    Protected WithEvents txtSearch As TextBox

    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim searchValue As String = txtSearch.Text.Trim()

        If Not String.IsNullOrEmpty(searchValue) Then
            SqlDataSource1.SelectCommand = "SELECT Narudžba.[ID narudžbe], Kupac.Ime, Narudžba.[Datum primitka], Narudžba.[Datum isporuke], Status.[Naziv statusa] FROM Narudžba INNER JOIN Kupac ON Narudžba.[ID kupac] = Kupac.[ID kupac] INNER JOIN Status ON Narudžba.[ID statusa] = Status.[ID statusa] WHERE [ID narudžbe] = @SearchValue"
            SqlDataSource1.SelectParameters.Clear()
            SqlDataSource1.SelectParameters.Add("SearchValue", searchValue)
        Else
            SqlDataSource1.SelectCommand = "PodaciZaTablicuNarudzbe"
            SqlDataSource1.SelectParameters.Clear()
        End If

        GridView1.DataBind()
    End Sub
End Class

The issue I'm facing is that when I enter a specific order number in the search input field and click the "Pretraži" button, the GridView doesn't update to display the filtered results. It seems like the btnSearch_Click event handler is not being triggered or the SQL query is not executed correctly.

I've double-checked the code and made sure that the button and textbox have the correct IDs. What could be causing this issue? Any suggestions on how to fix the search function?

Note: I'm using Visual Studio and SQL Server for this web application.

Thank you in advance for your help!


Solution

  • A few things:

    When I create a asp.net webforms page,

    I get this:

    <form id="form1" runat="server">
    
        <div>
    
        </div>
    
    
    </form>
    

    The default name is form1. Since the last 20 years of books, sample code, and that when you create a new page, the form tag defaults to form1. Is there any particular reason why you're changing this? (There might be a reason to do so, but I can't think of a good one. However, since you ARE changing that form tag id, then you need to have a good reason for doing so).

    Next up:

    When I drag + drop a button from the toolbox, I get this:

            <asp:Button ID="Button1" runat="server" Text="Button" />
    

    Again:

    Is there a huge great big fantastic earth shattering idea as to why you not using a asp.net button here?

    You CAN drop in a HTML button, but be careful, since onclick="client side java script code to run"

    In other words, for a HTML button, you want:

    a) a really good reason for making that choice of a control - not just random tossing mud against the wall, and hoping on a wing and prayer the button works.

    b) you want the button click to run JavaScript (client browser side code) to run for onclick="my java script function here".

    So, it not at all clear why you dragging and dropping in a HTML button here?

    So, for a HTML button, you can have this:

            <input id="Button2" type="button" value="button"
                onclick="myclientcode()"
                
                />
    
            <script>
                function myclientcode() {
                    alert("This is client side click")
                }
            </script>
    

    However, if you want code behind, and a server side event, then drag + drop in a asp.net button, and then simple double click on the button to create a code behind event.

    eg this:

    enter image description here

    And you can also do this while looking at markup, say like this:

    enter image description here

    I suppose the 3rd way if you love world poverty is to type in the markup by hand, but then again, I don't want you to starve to death by having to type in these things by hand. You can (and will certainly) after drag + drop of that button or control often tweak the resulting markup, and OFTEN will say change the button1 to a nice name like cmdEdit etc.

    However, in the case of my first point? No, you don't want to change the default form id tag of form1. (and one good reason is you SUPER RARE have to use that form1 "id" in code, and since 99% of the time you NEVER will use the form id tag in code, then you don't need to change the form id tag.

    However, for a control? Why of course you VERY OFTEN will use those controls and button in code behind, and thus it stands to reason that you often and should change the ID for those controls to your liking.

    So keep in mind;

     for HTML button, onclick="run client side java script code"
     for asp.net button, onclick="run server side code behind"
    

    Edit: A working search example

    So, now with all those issues we dealt with, let's try a search function. We will search for a hotel name, and then show the results in a grid view.

    So, our simple markup:

            <asp:Label ID="Label1" runat="server" Text="Enter Hotel" Font-Size="Large"></asp:Label>
            <asp:TextBox ID="txtHotel" runat="server" Font-Size="Large" Style="margin-left:20px"></asp:TextBox>
            <asp:Button ID="cmdSearch" runat="server" Text="Search" Style="margin-left:20px" CssClass="btn" />
    
            <br />
            <br />
    
            <asp:GridView ID="GridView1" runat="server" CssClass="table table-striped">
    
            </asp:GridView>
    

    And our code behind - note how we do NOT need those with events - again not clear why you adding/using them.

    So, the code behind is this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
            ' optional  - load grid with all data 
            LoadGrid()
        End If
    
    End Sub
    
    Sub LoadGrid()
    
        Dim strSQL As String =
            "SELECT * FROM vHotels WHERE HotelName like @Hotel + '%' 
             ORDER BY FirstName"
        Dim cmdSQL As New SqlCommand(strSQL)
        cmdSQL.Parameters.Add("@Hotel", SqlDbType.NVarChar).Value = txtHotel.Text
    
        GridView1.DataSource = MyRstP(cmdSQL)
        GridView1.DataBind
    
    End Sub
    
    Public Function MyRstP(cmdSQL As SqlCommand) As DataTable
    
        Dim rstData As New DataTable
        Using conn As New SqlConnection(My.Settings.TEST4)
            Using cmdSQL
                cmdSQL.Connection = conn
                conn.Open()
                rstData.Load(cmdSQL.ExecuteReader)
            End Using
        End Using
    
        Return rstData
    
    End Function
    

    And the result is this:

    enter image description here