In my site i have a search function in the master page (no defaultbutton set there, also not in form). in a content page, i have a login, there i use a asp panel with defaultbutton. but when i click enter on the login textbox then my site keeps going to the search event handler... What could be the reason?
Some code:
//on content page
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Button1.Text);
}
<asp:Panel ID="pnl1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:LinkButton ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
</asp:Panel>
//on master page:
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!txtSearch.Text.Equals(""))
{
Response.Redirect("searchresults.aspx?search=" + txtSearch.Text);
}
}
<div id="searchbar">
<asp:TextBox ID="txtSearch" CssClass="searchbar-field" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" CssClass="searchbar-btn" runat="server" Text="Zoek" OnClick="btnSearch_Click" />
</div>
OK found the solution: It is required to use Button
and not LinkButton
. Then it should be alright...
You just need to set the default button on the page on the page load:
You can access the button using the FindControl method of the panel (this is VB).
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Form.DefaultButton = pnl1.FindControl("Button1").UniqueID
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With