Search code examples
asp.netdefaultbutton

Setting DefaultButton to button.UniqueID throws exception


The problem I'm trying to solve:

I have several text boxes in an asp:Panel. When the user hits Enter from any of those boxes, I want the form to submit as if they've clicked btnAddTag. (When the cursor is not in those boxes, I have a different default submit button.)

The aspx:

<asp:Panel id="thePanel" runat="server">
    <asp:Button ID="btnAddTag" Text="Add Tag" runat="server" />
</asp:Panel>

The vb:

tagPanel.DefaultButton = btnAddTag.UniqueID

The exception:

The DefaultButton of 'tagPanel' must be the ID of a control of type IButtonControl.

The value of btnAddTag.UniqueID is ctl00$phMain$btnAddTag (there's a master page, this section is called phMain).

I've also tried CType(tagPanel.FindControl("btnAddTag"), Button).UniqueID.


Solution

  • do:

    tagPanel.DefaultButton = btnAddTag.ID
    

    more info here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx