[英]Enter Key firing off an Response.Redirect on aspx page
Hoping someone can help. 希望有人能提供帮助。 I am working on an aspx site, using the c# as the code behind.
我正在使用c#作为背后的代码在aspx网站上工作。 I have 3 input boxes, however when my cursor is in one of the input boxes, it seems to fire off the search_Click code which is fine if the cursor was in in the searchBox, but it seems to do it when the cursor is in the Username and Password Textbox.
我有3个输入框,但是,当光标位于输入框之一中时,似乎会触发search_Click代码,如果光标位于searchBox中就可以了,但是当光标位于用户名和密码文本框。 I dont have any javascript firing off this event, and what wanted to do was if user is in searchBox and hits enter, they fire the Search_Click otherwise if they are in either the username or password textbox and hit enter they fire off te code associated to login_Click.
我没有任何JavaScript触发此事件,而要执行的操作是,如果用户位于searchBox中,然后按Enter,则触发Search_Click;否则,如果它们位于用户名或密码文本框中,并按Enter,则它们触发与login_Click。 Hope that makes sense, anyone know why its firing the Response.Redirect even though I have no javascript/jquery teling it to.
希望这是有道理的,即使我没有将它连接到的javascript / jquery,任何人都知道为什么它会触发Response.Redirect。
//Front end aspx page
<input type="text" class="searchBox" autocomplete="off" id="searchBox" name="searchBox" runat="server" />
<asp:Button ID="searchBtn" class="searchBtn" runat="server" onclick="search_Click" />
<li>
<asp:Label ID="UserNameLabel" AssociatedControlID="UserName" runat="server" Text="Username :" CssClass="usernamelabel" />
<asp:TextBox ID="UserName" runat="server" ValidationGroup="RegisterValidationGroup" CssClass="Username-Password" />
</li>
<li>
<asp:Label ID="PasswordLabel" AssociatedControlID="Password" runat="server" Text="Password :" CssClass="usernamelabel" />
<asp:TextBox ID="Password" runat="server" ValidationGroup="RegisterValidationGroup" TextMode="Password" CssClass="Username-Password" />
</li>
<li>
<asp:Button ID="loginBtn" class="loginBtn" Text="Login" runat="server" onclick="Login_Click" />
</li>
//C# Code behind
protected void search_Click(object sender, EventArgs e)
{
Response.Redirect("/SearchResults.aspx?q=" + Server.UrlPathEncode(searchBox.Value));
}
You can try 你可以试试
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (event.keyCode!=13);" >
</asp:TextBox>
您可以在面板中包含不希望默认表单提交行为的文本框,并设置其DefaultButton属性。
You can create a Panel, put your search textBox and button into this panel and set DefaultButton="searchBtn" .
您可以创建一个面板,将搜索文本框和按钮放入该面板中,然后设置DefaultButton =“ searchBtn” 。 I refered to this link: https://stackoverflow.com/a/7836069/1380428
我引用了此链接: https : //stackoverflow.com/a/7836069/1380428
Unless I've misunderstood your question, you can use the DefaultButton
property of an asp:Panel
object 除非我误解了您的问题,否则可以使用
asp:Panel
对象的DefaultButton
属性。
For your login use something like... (untested) 对于您的登录,使用类似...(未测试)的信息
<asp:Panel runat="server" DefaultButton="btnLogin">
<asp:TextBox runat="server" id="txtUsername" />
<asp:TextBox runat="server" id="txtPassword" TextMode="Password"/>
<asp:Button runat="server" id="btnLogin" Text="Login" />
</asp:Panel>
And then for your search have... (untested) 然后为您的搜索...(未测试)
<asp:Panel runat="server" DefaultButton="btnSearch">
<asp:TextBox runat="server" id="txtSearch" />
<asp:Button runat="server" id="btnSearch" />
</asp:Panel>
The idea behind this is that if the focus is on a asp:TextBox
within the asp:Panel
, pressing enter will initiate the DefaultButton
. 其背后的想法是,如果焦点位于
asp:Panel
的asp:TextBox
,则按Enter键将启动DefaultButton
。 If you don't want a particular one to cause a post-back to the server, then update the DefaultButton
so that it automatically cancels, such as... 如果您不希望特定的按钮导致回发到服务器,请更新
DefaultButton
使其自动取消,例如...
<asp:Button runat="server" id="btnSearch" OnClientClick="return false;" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.