asp.netuser-controlsdefaultbutton

Multiple Default Buttons in a User Control


I have a user control that is comprised of 3 textboxes, 3 buttons and a gridview. I can set the defaultbutton property of the form, but that would only affect one button.

Each textbox/button combo should have a behavior that when the textbox has focus, pressing Enter should fire its associated button.

To further complicate the issue, this is being used inside a Master Page implementation.

Is there any way to do this? As far as I can tell, .NET will only allow one default button - it doesn't provide to explicitly associate textboxes and buttons.

I welcome your insights.


Solution

  • Here one way:

    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" DefaultButton="Button1" runat="server">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </asp:Panel>
        <asp:Panel ID="Panel2" DefaultButton="Button2" runat="server">
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button2" runat="server" Text="Button" />
        </asp:Panel>
        <asp:Panel ID="Panel3" DefaultButton="Button3" runat="server">
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button3" runat="server" Text="Button" />
        </asp:Panel>
    </div>
    </form>