ASP.NET DefaultButton and MasterPages

On my site, I have a search function on the main page (the default value is not set there, also not on the form). I have a login on the content page, there I use the asp panel with the default button. but when I press the enter button in the login text box, my site continues to work with the search event handler ... What could be the reason?

Some codes:

//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 a solution: it is required to use Button, but not LinkButton. Then it should be good ...

+5
source share
3 answers

You just need to set the default button on the page load page:

, FindControl ( VB).

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Me.Form.DefaultButton = pnl1.FindControl("Button1").UniqueID

End Sub
+2

, , Button NOT LinkButton. .

+2

, , html .

-, . , .

<form id="form1" runat="server" defaultbutton="ucLogin$btnSubmit">

(: ucLogin , . btnSubmit html )

DefaultButton:

<!-- Login Control - use a panel so we can set the default button -->
<asp:Panel runat="server" ID="loginControlPanel" DefaultButton="ucLogin$btnSubmit">                         
     <uc:Login runat="server" ID="ucLogin"/>                                                    
</asp:Panel>

.

0

All Articles