处理
发布于 2024-10-02 12:52:46 字数 322 浏览 1 评论 0 原文

我有一个 asp.net 文本框,我想将其用作搜索框。

我不打算有一个按钮,只是允许用户在文本框中输入搜索关键字并按 Enter 键。

<div id="search-bar">
    <asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox>
</div>

如何获得“按回车键”来调用方法,或者回发带有 URL 参数中的关键字的页面,例如 search.aspx?keywords=this&that

I have an asp.net textbox which I want to use as a search box.

I wasn't planning on having a button, just allowing the user to type their search keywords into the textbox and press enter.

<div id="search-bar">
    <asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox>
</div>

How can I get the "enter press" to call a method, or post back the page with keywords in URL parameters, e.g. search.aspx?keywords=this&that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橙幽之幻 2024-10-09 12:52:46

Set AutoPostback to true if you want to call a function in codebehind OnTextChanged. This will occur if the Textbox loses focus(f.e. Tab-key) or Enter-Key is pressed.

北恋 2024-10-09 12:52:46

还有其他方法可以使用表单对象的 DefaultButton 属性或面板的 DefaultButton 属性设置默认按钮,但我发现它们过去在各种浏览器中不可靠,所以通常我依赖 javascript,如果你不这样做想要按钮可见,只需将visible属性设置为false即可。

该代码的唯一缺点是您必须使用页面指令关闭事件验证,但它应该触发单击事件,并触发验证器等等。

这是我们使用的代码示例。通常我会将寄存器函数放在实用程序类中,但对于本例来说,它位于页面代码中。

<%@ Page Language="C#" EnableEventValidation="false" %>

<script runat="server">

    protected void cmdSubmit1_Click(object sender, EventArgs e)
    {
        litValue.Text = "Value 1 - You entered: " + txtValue1.Text;
    }
    protected void cmdSubmit2_Click(object sender, EventArgs e)
    {
        litValue.Text = "Value 2 - You entered: " + txtValue2.Text;
    }

    /// <summary>
    /// This function registers what button is clicked based on whatever control currently has focus
    /// so for example if the user password field has focus then you can cause the enter button to click
    /// if the enter key is pressed This works with ie and firefox as far as I know
    /// </summary>
    /// <param name="ControlWithFocus"></param>
    /// <param name="ControlToClick"></param>
    private void RegisterDefaultButton(System.Web.UI.Control ControlWithFocus, System.Web.UI.Control ControlToClick)
    {

        PostBackOptions p = new PostBackOptions(ControlToClick);
        p.PerformValidation = true;
        if (ControlToClick is Button)
        {
            p.ValidationGroup = ((Button)ControlToClick).ValidationGroup;
        }
        else if (ControlToClick is ImageButton)
        {
            p.ValidationGroup = ((ImageButton)ControlToClick).ValidationGroup;
        }
        else if (ControlToClick is LinkButton)
        {
            p.ValidationGroup = ((LinkButton)ControlToClick).ValidationGroup;
        }

        p.RequiresJavaScriptProtocol = false;

        AttributeCollection a = null;
        if (ControlWithFocus is HtmlControl)
        {
            a = ((System.Web.UI.HtmlControls.HtmlControl)ControlWithFocus).Attributes;
        }
        else if (ControlWithFocus is WebControl)
        {
            a = ((System.Web.UI.WebControls.WebControl)ControlWithFocus).Attributes;
        }

        if (a != null)
        {
            a["onKeyDown"] = string.Format("if (event.keyCode == 13) {{{0}}}"
                  , ControlToClick.Page.ClientScript.GetPostBackEventReference(p));
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterDefaultButton(txtValue1, cmdSubmit1);
        RegisterDefaultButton(txtValue2, cmdSubmit2);

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        Enter Value 1: <asp:TextBox ID="txtValue1" runat="server"></asp:TextBox>
        <br />
        Enter Value 2: <asp:TextBox ID="txtValue2" runat="server"></asp:TextBox>
        <br />
        <asp:Literal ID="litValue" runat="server"></asp:Literal>
        <asp:LinkButton ID="cmdSubmit1" runat="server" Visible="false" OnClick="cmdSubmit1_Click">Hidden Button 1</asp:LinkButton>
        <input id="cmdSubmit2" runat="server" visible="false" type="button" value="Hidden Button 2" onserverclick="cmdSubmit2_Click" />
    </form>
</body>
</html>

There are other ways you can set a default button using the form object DefaultButton property or the DefaultButton property of a panel, but I have found them to be unreliable in the past in various browsers so usually I rely on javascript, if you don't want the button visible you can just set the visible property to false.

The only downside to this code is you have to turn off event validation with a page directive, but it should fire off click events, and trigger validators and all that.

Here is an example the code that we use. Normally I would put the register function in a utility class, but for this example it is in the page code.

<%@ Page Language="C#" EnableEventValidation="false" %>

<script runat="server">

    protected void cmdSubmit1_Click(object sender, EventArgs e)
    {
        litValue.Text = "Value 1 - You entered: " + txtValue1.Text;
    }
    protected void cmdSubmit2_Click(object sender, EventArgs e)
    {
        litValue.Text = "Value 2 - You entered: " + txtValue2.Text;
    }

    /// <summary>
    /// This function registers what button is clicked based on whatever control currently has focus
    /// so for example if the user password field has focus then you can cause the enter button to click
    /// if the enter key is pressed This works with ie and firefox as far as I know
    /// </summary>
    /// <param name="ControlWithFocus"></param>
    /// <param name="ControlToClick"></param>
    private void RegisterDefaultButton(System.Web.UI.Control ControlWithFocus, System.Web.UI.Control ControlToClick)
    {

        PostBackOptions p = new PostBackOptions(ControlToClick);
        p.PerformValidation = true;
        if (ControlToClick is Button)
        {
            p.ValidationGroup = ((Button)ControlToClick).ValidationGroup;
        }
        else if (ControlToClick is ImageButton)
        {
            p.ValidationGroup = ((ImageButton)ControlToClick).ValidationGroup;
        }
        else if (ControlToClick is LinkButton)
        {
            p.ValidationGroup = ((LinkButton)ControlToClick).ValidationGroup;
        }

        p.RequiresJavaScriptProtocol = false;

        AttributeCollection a = null;
        if (ControlWithFocus is HtmlControl)
        {
            a = ((System.Web.UI.HtmlControls.HtmlControl)ControlWithFocus).Attributes;
        }
        else if (ControlWithFocus is WebControl)
        {
            a = ((System.Web.UI.WebControls.WebControl)ControlWithFocus).Attributes;
        }

        if (a != null)
        {
            a["onKeyDown"] = string.Format("if (event.keyCode == 13) {{{0}}}"
                  , ControlToClick.Page.ClientScript.GetPostBackEventReference(p));
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterDefaultButton(txtValue1, cmdSubmit1);
        RegisterDefaultButton(txtValue2, cmdSubmit2);

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        Enter Value 1: <asp:TextBox ID="txtValue1" runat="server"></asp:TextBox>
        <br />
        Enter Value 2: <asp:TextBox ID="txtValue2" runat="server"></asp:TextBox>
        <br />
        <asp:Literal ID="litValue" runat="server"></asp:Literal>
        <asp:LinkButton ID="cmdSubmit1" runat="server" Visible="false" OnClick="cmdSubmit1_Click">Hidden Button 1</asp:LinkButton>
        <input id="cmdSubmit2" runat="server" visible="false" type="button" value="Hidden Button 2" onserverclick="cmdSubmit2_Click" />
    </form>
</body>
</html>
流云如水 2024-10-09 12:52:46

使用 javascript 提交表单的方法是:

document.forms["myform"].submit();

但是 asp 通常会在单击按钮时放置一大堆 javascript 来处理视图状态和类似的事情,因此您最好添加一个设置为表单默认值的按钮,然后用CSS隐藏它。

to submit a form with javascript is:

document.forms["myform"].submit();

but asp usually puts a whole bunch of javascript in the click for buttons to do with viewstate and things like that, so you might be better off adding a button that is set as the default for the form, and then hiding it with CSS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文