简体   繁体   English

输入文本框时,Asp.net按钮单击服务器事件

[英]Asp.net buttonclick server event firing when textbox is entered

I have a textbox and a button. 我有一个文本框和一个按钮。

The button has a btn_click(obj, eventargs) on codebehind. 该按钮在代码隐藏上有一个btn_click(obj, eventargs)

This is on master page. 这是在master页上。

What I want is when the textbox is focused and click enter , 我想要的是当文本框被聚焦并单击enter

I want to run that function, in other words, click the button. 我想运行该功能,换句话说,单击按钮。

So, I wrote... 所以,我写了......

$("#" + "<%= txtSearch.ClientID %>").keypress(function (e) {
  var code = (e.keyCode ? e.keyCode : e.which);
  if (code == 13) {
    $("#" + "<%= imgBtnSearch.ClientID %>").click();
}

On server side function, I am redirecting to another page. 在服务器端功能,我正在重定向到另一个页面。

Since it is not redirecting, can i assume that it is not working? 由于它没有重定向,我可以认为它不起作用吗?

Is there anything I can make it work? 有什么我可以使它工作?

}); });

This is it: 就是这个:

<script type="text/javascript">
    $(function () {
        $("#" + "<%= txtSearch.ClientID%>").keypress(function (e) {
          var code = (e.keyCode ? e.keyCode : e.which);
          if (code == 13) {
            e.preventDefault();
            $("#" + "<%= imgBtnSearch.ClientID%>").click();
        }});
    });
</script>
<form id="Form1" runat="server">
   <asp:Panel ID="Pnl1" runat="server" DefaultButton="BtnTest">
   <asp:TextBox ID="TxtTest" runat="server"></asp:TextBox>
   <asp:Button ID="BtnTest" runat="server" Text="Test" OnClick="BtnTest_Click" />
   </asp:Panel>
</form>   

Try this this will trigger the "BtnTest" only for the textbox "TxtTest" 试试这个,只会为文本框“TxtTest”触发“BtnTest”

You should try enclose the Textbox and the Button in a Panel and set the DefaultButton property for the panel as the button. 您应该尝试在Panel包含TextboxButton ,并将Panel的DefaultButton属性设置为按钮。 This will ensure that when you press enter in the textbox the button click would be triggered 这将确保当您在文本框中按Enter键时,将触发按钮单击

Or you can do it in another fashion by setting the DefaultButton for the form like Page.Form.DefaultButton = [yourbutton] 或者您可以通过为Page.Form.DefaultButton = [yourbutton]等表单设置DefaultButton以另一种方式执行此操作。

william! 威廉!

Please provide a server side function code, may be problem is there. 请提供服务器端功能代码,可能存在问题。 If it's ok with server-side handler, then you should open JavaScript console and check if there's any errors (If so, then redirect won't occur). 如果服务器端处理程序没问题,那么您应该打开JavaScript控制台并检查是否有任何错误(如果是,则不会发生重定向)。

AND try this function instead of yours to cause a button server click. 并尝试此功能而不是您的功能导致按钮服务器单击。

$("#" + "<%= txtSearch.ClientID %>").keypress(function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        <%= scriptPostBackButton1 %>
    }
}

In this example, hitting 'enter' down on textbox causes server-side click handler with postback. 在此示例中,在文本框上按下“enter”会导致服务器端单击处理程序带回发。 Is this a behavior you need? 这是你需要的行为吗?

Code-behind: 代码隐藏:

public partial class _Default : System.Web.UI.Page
{
    public string scriptPostBackButton1 = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            scriptPostBackButton1 = 
                ClientScript.GetPostBackEventReference(new PostBackOptions(clickableButton, "", "", false, false, true, true, true, ""));
        }
    }


    protected void clickableButton_Click(object sender, EventArgs e)
    {
        string result = "ok, it's working";            
    }
}

Default.aspx Default.aspx的

<form id="Form1" runat="server">
    <asp:TextBox ID="tbEnter" runat="server" onkeydown=" return clientHandler(event);"  />
    <asp:Button ID="clickableButton" runat="server" Text="click" Width="49px"  />
</form>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSON-asp.net中的ButtonClick无法启动? - JSON - ButtonClick in asp.net is not firing? ASP.NET在更改文本框,触发客户端事件和服务器事件后单击按钮 - ASP.NET clicking on button after changing textbox, firing client event and server event 当浏览器 JavaScript 禁用时,TextBox textchanged 事件在 asp.net 中不起作用/未触发 - TextBox textchanged event is not working/not firing in asp.net When browser JavaScript disable 在文本框中使用jquery和html5 required属性时,asp.net按钮事件未触发 - asp.net button event not firing when using jquery and html5 required attribute in textbox ASP.NET Arraylist,ButtonClick事件,单击按钮时从arraylist检索字符串值 - ASP.NET Arraylist, ButtonClick event, retireve string value from the arraylist when button is clicked ASP.NET事件未触发 - ASP.NET Event not firing ASP.NET按钮单击事件未在Web服务器上触发 - asp.net button click event not firing on web server ASP.Net自定义服务器控件未触发自定义事件 - ASP.Net Custom Server Control Not Firing Custom Event asp.net自定义验证器没有为文本框触发 - asp.net custom validator not firing for textbox 使用modelpopupextender时未触发ASP.NET按钮事件 - ASP.NET button event not firing when using modelpopupextender
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM