在按键上设置 Enter 以开始搜索 asp.net c#

Set Enter on keypress to start search asp.net c#

我有一个带有 GridView 的页面和一个靠近它的按钮的搜索文本框,我想将其设置为当用户按“ENTER”开始搜索时,仅此而已。 我不知道如何在 ASP.NET C# 中设置这个东西。

以及如何修改样式:

Messaggio_Ricerca.Text = " I caratteri inseriti nella barra di ricerca non sono validi. "

什么时候出来?

ASP代码:

     Ricerca :
            <asp:TextBox ID="CampoRicerca" runat="server" CssClass="textbox" Width="300px" Height="25px" Placeholder="Inserisci qui i dati che vuoi cercare" ></asp:TextBox>
            <asp:Button ID="BTN_Ricerca" OnClick="Ricerca_NomeInserito" runat="server" Text="" Width="35" Height="35" CssClass="ricerca"></asp:Button>
            <asp:Label ID="Messaggio_Ricerca" runat="server"></asp:Label>

C#代码:

protected void Ricerca_NomeInserito(object sender, EventArgs e)
        {
            this.RicercaCliente();
        }

private void RicercaCliente()
        {
            //String query code

            if (String.IsNullOrWhiteSpace(CampoRicerca.Text))
            {
                Messaggio_Ricerca.Text = " I caratteri inseriti nella barra di ricerca non sono validi. ";
            }

            else
            {
                //SqlCommand code
            }
       }

好的,假设我们有这个标记:

一个简单的网格和一个搜索:

<asp:Label ID="Label1" runat="server" Text="Search for Fighter jet" Font-Size="Large"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server" Style="margin-left:15px" Font-Size="Large"></asp:TextBox>
<asp:Button ID="cmdSearch" runat="server" Text="search"
    style="margin-left:15px" CssClass="btn" OnClick="cmdSearch_Click"
    />
<br />
<br />

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" CssClass="table"  >
        <Columns>
            <asp:BoundField DataField="Fighter" HeaderText="Fighter"  />
            <asp:BoundField DataField="Engine" HeaderText="Engine"  />
            <asp:BoundField DataField="Thrust" HeaderText="Thrust"  />
            <asp:BoundField DataField="Description" HeaderText="Description" />

            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                <asp:ImageButton ID="btnImage" runat="server" Height="68px" Width="149px"
                    OnClientClick ="popimage(this);return false"
                    ImageUrl = '<%# Eval("ImagePath") %>' /> 
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我们要加载的代码是这样的:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid("");
    }

    void LoadGrid(string MySearch)
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmdSQL = new SqlCommand("SELECT * from Fighters ", conn))
            {
                if (MySearch != "")
                {
                    cmdSQL.CommandText += @" WHERE Fighter LIKE @Fighter + '%'";
                    cmdSQL.Parameters.Add("Fighter", SqlDbType.NVarChar).Value = MySearch;
                }
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmdSQL.ExecuteReader());
                GridView1.DataSource = rstData;
                GridView1.DataBind();
            }
        }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        LoadGrid(txtSearch.Text);
    }

现在我们有了这个:

所以,不是输入搜索文本然后点击搜索按钮吗?

当然可以,让我们放下(删除)搜索按钮。现在我们使用文本框的text changed事件,我们设置auto postback = true.

所以,我们现在有了这个标记:

<asp:Label ID="Label1" runat="server" Text="Search for Fighter jet" Font-Size="Large"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server" Style="margin-left:15px" Font-Size="Large"
    AutoPostBack="true" OnTextChanged="txtSearch_TextChanged" >
</asp:TextBox>
    
<br />
<br />

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

因此,将文本框设置为 auto postback = true,那么它的所有事件都将触发 - 并触发事件。

后面的代码呢?我们获取按钮代码并将其移至文本更改事件,如下所示:

    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        LoadGrid(txtSearch.Text);
    }

所以现在我们只需输入一些文本,然后按回车键,我们得到: