获取输入键以提交右侧文本框的按钮 - 两个带有两个提交按钮的文本框

时间:2016-02-02 20:14:06

标签: asp.net

我正在编写一个asp web应用程序。它是一个页面,用作搜索引擎,通过sql将一些数据显示到gridview中。 数据有两个元素 - 代码和描述。您可以通过代码或描述来搜索它。所以我有一个代码文本框和按钮,以及一个描述文本框和按钮。但按下回车键仅提交搜索页面上的第一个按钮(代码)。

有没有办法让我按下回车键提交与他们当前所在文本框对应的按钮?

    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

1 个答案:

答案 0 :(得分: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
}