Setting DefaultButton to button.UniqueID throws exception

3.8k views Asked by At

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.

2

There are 2 answers

2
Victor On BEST ANSWER

do:

tagPanel.DefaultButton = btnAddTag.ID

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

0
kolbasov On

You should set the control's ID not UniqueID:

tagPanel.DefaultButton = btnAddTag.ID