Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET DefaultButton and MasterPages

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...

like image 767
Ozkan Avatar asked Oct 11 '22 23:10

Ozkan


1 Answers

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
like image 100
Ira Rainey Avatar answered Oct 20 '22 14:10

Ira Rainey