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 shoul开发者_StackOverflow社区d 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.
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>
Try creating a separate User Control for each TextBox/Button set. That way each Textbox will fire it's associated Button.
You can disable the default button and handle the textbox's onKeyPress event.
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:
.
精彩评论