0

So i am currently trying to to a textbox with an image inside, and i have the method onclick to enter my event of button_clink in c# but its not working

<div class="input-container">
    <input type="text" id="txtSearch" runat="server" class="input-field" style="color: black;" placeholder="Pesquisar.."/>
    <button id="btnsearch" class="wrapper" OnClick="btnsearch_Click" runat="server"><i class="fas fa-search" style="color:#8b9095;" ></i></button>
</div>

c# Code:

protected void btnsearch_Click(object sender, EventArgs e)
{
    con.Open();
    var txtSearch = FindControl("txtSearch") as TextBox;
    SqlCommand cmd = new SqlCommand("select DISTINCT from [encomenda] where No_= " + txtSearch + "", con);

    SqlDataReader sdr = cmd.ExecuteReader();

    if (sdr.Read())
    {
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    con.Close();
}
mason
  • 31,774
  • 10
  • 77
  • 121
  • Is there a `btnsearch_Click` function in your Javascript code? – Alejandro Jun 16 '21 at 13:11
  • no im not using Javascript, i am using c# – Miguel Araujo Jun 16 '21 at 13:14
  • C# runs in the server, your snippet is a chunk of HTML, which runs in a browser, therefore the `OnClick` will always refer to client-side Javascript code. C# doesn't exists there. For calling the server, you need to make a GET/POST request. – Alejandro Jun 16 '21 at 13:18
  • and how do i make that? – Miguel Araujo Jun 16 '21 at 13:19
  • 1
    @Alejandro Perhaps you're not familiar with Web Forms, but indeed OnClick can trigger a postback with Web Forms and invoke a server side function. If you're not familiar with Web Forms, it might be best to refrain from answering here as you'll confuse the situation. – mason Jun 16 '21 at 13:29
  • Please show your server side code that you're trying to reach. Also, have you considered using an `asp:Button` control instead of an `input`? – mason Jun 16 '21 at 13:30
  • @mason yes but then i cant use that image i have on the button – Miguel Araujo Jun 16 '21 at 13:31
  • Sure you can. Anyways, let's talk about your server side logic. You do a redirect first thing. Why? By redirecting away, there's no point in binding data to a GridView because the user is going to be redirected to a different page. Why do you have an empty catch block? The effect of that is if something goes wrong, it's going to swallow the exception and you won't know anything bad happened. That's an anti-pattern you need to get rid of. – mason Jun 16 '21 at 13:36
  • 1
    Your connection handling needs work too, you should use a [using statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement). You shouldn't declare your SqlConnection or almost any other item that implements [IDisposable](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-5.0) as a field in your code. Create and open the connection in a using statement so you can ensure it gets disposed of properly. – mason Jun 16 '21 at 13:37
  • oh sorry ignore that redirect and the catch is ther so i know the errors when i debug – Miguel Araujo Jun 16 '21 at 13:38
  • I'm not going to ignore that redirect, as it has implications for the processing. If its no longer in your actual code, then you need to remove it in your question. Your code in your question needs to reflect your actual code. Otherwise we're going to have a very hard time helping you. Have you tried setting a breakpoint to see if your server side code is reached? Otherwise how are you able to know if your code was reached or not? – mason Jun 16 '21 at 13:38
  • Yes i have already tried the breakpoint put he doesnt reach it anyway – Miguel Araujo Jun 16 '21 at 13:40
  • Where did you put the breakpoint? You still need to remove the try/catch, or at least put a logging statement in the catch block. Empty catch blocks are evil and don't belong in your code. What happens when you click the button? Does a postback occur? Are there any client side JavaScript errors? – mason Jun 16 '21 at 13:41
  • i putted the break point at the load event, ok i will get rid of that try catch – Miguel Araujo Jun 16 '21 at 13:42
  • 1
    You've got a SQL Injection vulnerability in your code. That's something you need to fix, otherwise you'll get a visit from [Little Bobby Tables](https://bobby-tables.com/). Always parameterize your queries, if you click on ADO.NET on that link, it will explain how to do that. – mason Jun 16 '21 at 13:46
  • wich link are you talking about? – Miguel Araujo Jun 16 '21 at 13:54
  • ohhhh nic ei gounf it now – Miguel Araujo Jun 16 '21 at 16:06
  • i have been using the sqlassparamter in ike 80º5º – Miguel Araujo Jun 16 '21 at 16:06
  • 80% of my pages front and back – Miguel Araujo Jun 16 '21 at 16:07

0 Answers0