Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 处理输入按入<;asp:文本框_Asp.net_Form Submit - Fatal编程技术网

Asp.net 处理输入按入<;asp:文本框

Asp.net 处理输入按入<;asp:文本框,asp.net,form-submit,Asp.net,Form Submit,我有一个asp.net文本框,我想用作搜索框 我不打算有一个按钮,只允许用户在文本框中键入他们的搜索关键字,然后按enter键 <div id="search-bar"> <asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox> </div> 如何使用“回车键”调用方法,或使用URL参数中的关键字发回页面,例如search.aspx?keywords=this&that?使用

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

我不打算有一个按钮,只允许用户在文本框中键入他们的搜索关键字,然后按enter键

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


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

使用javascript提交表单:

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

但是asp通常会在点击按钮中添加一大堆javascript来处理viewstate之类的事情,因此最好添加一个设置为表单默认值的按钮,然后用CSS隐藏它。

如果要在codebehind中调用函数,请将其设为true。如果文本框失去焦点(如Tab键)或按下Enter键,则会出现这种情况

您可以通过其他方式使用窗体对象DefaultButton属性或面板的DefaultButton属性设置默认按钮,但我发现它们在过去的各种浏览器中都不可靠,因此我通常依赖javascript,如果不希望按钮可见,可以将visible属性设置为false

这段代码唯一的缺点是必须使用page指令关闭事件验证,但它应该触发click事件,并触发验证器等等

下面是我们使用的代码示例。通常我会把register函数放在一个实用程序类中,但在本例中,它位于页面代码中

<%@ 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>

受保护的void cmdSubmit1\u单击(对象发送方,事件参数e)
{
litValue.Text=“值1-您输入:”+txtValue1.Text;
}
受保护的void cmdSubmit2\u单击(对象发送方,事件参数e)
{
litValue.Text=“值2-您输入:”+txtValue2.Text;
}
/// 
///此函数根据当前具有焦点的控件注册单击的按钮
///因此,例如,如果用户密码字段具有焦点,则可以使enter按钮单击
///据我所知,如果按下回车键,ie和firefox都能正常工作
/// 
/// 
/// 
私有无效注册表默认按钮(System.Web.UI.Control控件带焦点,System.Web.UI.Control控件单击)
{
PostBackOptions p=新的PostBackOptions(ControlToClick);
p、 性能验证=真;
如果(ControlToClick是按钮)
{
p、 ValidationGroup=((按钮)ControlToClick).ValidationGroup;
}
else if(ControlToClick是图像按钮)
{
p、 ValidationGroup=((ImageButton)ControlToClick).ValidationGroup;
}
else if(ControlToClick是链接按钮)
{
p、 ValidationGroup=((链接按钮)ControlToClick.ValidationGroup;
}
p、 RequiresJavaScriptProtocol=false;
AttributeCollection a=null;
if(ControlWithFocus为HtmlControl)
{
a=((System.Web.UI.HtmlControls.HtmlControl)ControlWithFocus)属性;
}
else if(ControlWithFocus为网络控制)
{
a=((System.Web.UI.WebControls.WebControl)ControlWithFocus)属性;
}
如果(a!=null)
{
a[“onKeyDown”]=string.Format(“if(event.keyCode==13){{{{0}}}”
,ControlToClick.Page.ClientScript.GetPostBackEventReference(p));
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
RegisterDefaultButton(txtValue1,cmdSubmit1);
RegisterDefaultButton(txtValue2,cmdSubmit2);
}
输入值1:

输入值2:
隐藏按钮1