0

I'm writing an asp web application. It is one page, and functions as a search engine to bring up some data through sql into a gridview. There are two elements to the data - its code, and its description. You can either search for it by code, or by description. So I have a code textbox and button, and a description textbox and button. But pressing the enter key only submits the search for the first button on the page (code).

Is there any way I can make hitting the enter key submit the button that corresponds with that textbox that they are currently in?

    Protected Sub Button1_click(sender As Object, e As EventArgs) Handles Button1.Click    
          Dim tCodeText As String

      Label1.Text = ""

     TextBox2.Text = ""


      If String.IsNullOrWhiteSpace(TextBox1.Text) Then

        GridView1.Columns.Clear()

        GridView1.DataBind()


      Else
        tCodeText = TextBox1.text

        SqlDataSource1.SelectCommand = "SELECT * FROM [tCodes] WHERE [TCode] LIKE '%" + tCodeText + "%'"

        GridView1.Visible = "True"


        If Gridview1.Rows.Count = 0 Then
           Label1.Text = "No results meet your search criteria"
        End If

      End If


    End Sub

    Protected Sub Button2_click(sender As Object, e As EventArgs) Handles Button2.Click

      Dim descText As String
      Dim splitArray() As String
      Dim sqlText As String

      Label1.Text = "" 

    TextBox1.Text = ""


      If String.IsNullOrWhiteSpace(TextBox2.Text) Then

GridView1.Columns.Clear()

        GridView1.DataBind()

      Else
        DescText = TextBox2.text

        splitArray = Split(descText)

        sqlText = "SELECT * FROM [tCodes] WHERE [Description] LIKE '%" + splitArray(0) + "%'"

        For index = 0 To splitArray.Length - 1
          sqlText += "OR [Description] LIKE '%"
          sqlText += splitArray(index)
          sqlText += "%' "
        Next

        SqlDataSource1.SelectCommand = sqlText

        GridView1.Visible = "True"

If Gridview1.Rows.Count = 0 Then
        Label1.Text = "No results meet your search criteria"
      End If

      End If


    End Sub
jhj
  • 15
  • 1
  • 4

1 Answers1

0

IF you are using MVC, follow here. This is best way, I think.

If you are not using MVC, if you are using classic ASP, follow the following example code:

<asp:Panel ID="pnl1" runat="server" DefaultButton="Button1">
    <asp:TextBox ID="TextBox1" runat="server" />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
</asp:Panel>

<asp:Panel ID="pnl2" runat="server" DefaultButton="Button2">
    <asp:TextBox ID="TextBox2" runat="server" />
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" />
</asp:Panel>

I think it is best way. Second solution:

  • Use the same method to OnClick in your buttons, and in that method use TextBoxes:

    <asp:TextBox ID="TextBox1" runat="server" />
    <asp:Button ID="Button1" runat="server" OnClick="Button_Click" />
    <asp:TextBox ID="TextBox2" runat="server" />
    <asp:Button ID="Button2" runat="server" OnClick="Button_Click" />
    

And your code behind (for second solution):

protected void Button_Click(object sender, EventArgs e){
    if (String.IsNullOrEmpty(TextBox1.Text))
        Method2();
    else
        Method1();
}

protected void Method1(){
    //your actions here
}

protected void Method2(){
    //your actions here
}
Community
  • 1
  • 1
Khazratbek
  • 1,656
  • 2
  • 10
  • 17