Reputation: 853
Hoping someone can help. I am working on an aspx site, using the c# as the code behind. 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. 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. Hope that makes sense, anyone know why its firing the Response.Redirect even though I have no javascript/jquery teling it to.
//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));
}
Upvotes: 0
Views: 2691
Reputation: 19963
Unless I've misunderstood your question, you can use the DefaultButton
property of an asp:Panel
object
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
. 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...
<asp:Button runat="server" id="btnSearch" OnClientClick="return false;" />
Upvotes: 0
Reputation: 8696
You can create a Panel, put your search textBox and button into this panel and set DefaultButton="searchBtn". I refered to this link: https://stackoverflow.com/a/7836069/1380428
Upvotes: 0
Reputation: 223322
You can try
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (event.keyCode!=13);" >
</asp:TextBox>
Upvotes: 1
Reputation: 1541
You can include the text boxes that do not want the default form submit behaviour in a panel and set its DefaultButton property.
Upvotes: 0