2010-11-17 40 views
3

我有一个asp.net文本框,我想用作搜索框。处理进入按<asp:textbox

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

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

我怎样才能获得“进入按”调用一个方法,或回来后页面与URL参数,例如关键字search.aspx?keywords=this&that

回答

0

提交使用JavaScript的形式是:

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

但ASP通常把一大堆的JavaScript在点击按钮做的ViewState之类的东西,所以你可能会更好添加按钮,该按钮被设置为表单的默认值,然后用CSS隐藏它。

6

如果您想调用代码隐藏OnTextChanged中的函数,请将AutoPostback设置为true。如果文本框失去焦点(如:Tab -key)或Enter - 键被按下会发生这种情况。

0

还有其他一些方法可以使用表单对象的DefaultButton属性或面板的DefaultButton属性来设置默认按钮,但我发现它们在各种浏览器中过去不可靠,所以通常我依赖于javascript,if你不希望按钮可见,你可以将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>