4

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.

H H
  • 263,252
  • 30
  • 330
  • 514
Al.
  • 43
  • 1
  • 3

4 Answers4

4

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>
Saar
  • 8,286
  • 5
  • 30
  • 32
0

Try creating a separate User Control for each TextBox/Button set. That way each Textbox will fire it's associated Button.

jinsungy
  • 10,717
  • 24
  • 71
  • 79
  • I see what you mean, but to me that overcomplicates the page. I might as well just have the users just click the associated button. I have them doing that now anyway. – Al. Jan 11 '10 at 16:45
0

You can disable the default button and handle the textbox's onKeyPress event.

David
  • 5,356
  • 2
  • 26
  • 39
0

Here is a sweet solution by usage of an Attached Property: http://neilmosafi.blogspot.com/2007/04/default-buttons-in-wpf-and-multiple.html

Just create a class with the attached property code of the article and add it to controls, which encapsulate each TextBox and Button pair.

Example: <StackPanel ... local:AccessKeyScoper.IsAccessKeyScope="True"> <TextBox ... /> <Button ... /> </StackPanel>

Note: If you add the file with the Dependency Property to a subdirectory, you need to use an other namespace definition instead of local:.

Beauty
  • 865
  • 11
  • 14