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));
}
Unless I’ve misunderstood your question, you can use the
DefaultButtonproperty of anasp:PanelobjectFor your login use something like… (untested)
And then for your search have… (untested)
The idea behind this is that if the focus is on a
asp:TextBoxwithin theasp:Panel, pressing enter will initiate theDefaultButton. If you don’t want a particular one to cause a post-back to the server, then update theDefaultButtonso that it automatically cancels, such as…