How to avoid InvalidOperationException when setting the defaultbutton in ASPX content
Asked Answered
J

4

1

I am trying to set a default button in my ASPX page. I have a master page so the form is there. I have a panel in the content page that holds a table that organizes a number of textboxes, dropdowns and other inputs. The last row of the table holds some buttons, one of which I want to be the default button. After doing some research, I have tried the following with no success.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
       pnlHolder.DefaultButton = cmdSearchJob.ClientID

I have also tried

pnlHolder.DefaultButton = cmdSearchJob.UniqueID

and

Dim cmdDef As Button = pnlHolder.FindControl("cmdSearchJob")
pnlHolder.DefaultButton = cmdDef.UniqueID

but both throw the exception "The DefaultButton of 'pnlHolder' must be the ID of a control of type IButtonControl.".

I have seen some Javascript solutions, but was hoping to just be able to set the defaultButton for the panel.

Jarad answered 22/7, 2010 at 20:21 Comment(0)
J
0

Finally found what it was. Tried adding another button with no CSS, etc. that just popped up a javacsript alert() for testing. Was able to set that as the default button when it was outside the table. As the submit button is one of a series of buttons (not a very pretty UI, but user requirements and all that) in the table, I used this additional button and set its style to display:none and have it call the same subroutine in the code behind.

So, short answer, it wasn't seeing it in the table.

Thanks everyone for your input.

I still have no idea why it wouldn't set the button.

Jarad answered 23/7, 2010 at 14:22 Comment(1)
I have the same problem, but my button is not in a table, just in a <p> on an <asp:Content> control. I use a master page, if this is relevant.Racehorse
A
2

Try setting the DefaultButton of the parent Form.

C#:

 this.Page.Form.DefaultButton = cmdSearchJob.UniqueID;

VB?:

me.Page.Form.DefaultButton = cmdSearchJob.UniqueID

Similar issue here: Allow Enter key to login in asp.net?

Access answered 22/7, 2010 at 20:45 Comment(2)
Tried this and got no error but no defaultButton behavior either.Jarad
Worked perfectly for me. I first tried ClientID, but the mentioned "UniqueID" worked finally.Racehorse
E
0

Try setting it to:

cmdSearchJob.ID

The panel will call FindControl to get the Client ID itself

Euxenite answered 22/7, 2010 at 20:25 Comment(1)
Doesn't work. Don't get the error but don't get the defaultButton behavior either. Because of the Master Page, the rendered ID is "ctl00_ContentPlaceHolder1_cmdSearchJob". And hardcoding it in either the code or directly in the pnlHolder properties results in the same error.Jarad
G
0

Can you set this inside the control on the front-end?

<asp:Panel id="pnlHolder" DefaultButton="cmdSearchJob">
<asp:Button id="cmdSearchJob" runat="server" Text="Search" />
</asp:Panel>

Also, this may worth knowing, what type of object is cmdSearchJob? Is it a standard asp.net button control?

Garrulous answered 22/7, 2010 at 21:5 Comment(1)
Doing this causes the same error (The DefaultButton of 'pnlHolder' must be the ID of a control of type IButtonControl). cmdSearchJob is just a plain vanilla ASP button. It has a CSS style applied to provide left and right margins.Jarad
J
0

Finally found what it was. Tried adding another button with no CSS, etc. that just popped up a javacsript alert() for testing. Was able to set that as the default button when it was outside the table. As the submit button is one of a series of buttons (not a very pretty UI, but user requirements and all that) in the table, I used this additional button and set its style to display:none and have it call the same subroutine in the code behind.

So, short answer, it wasn't seeing it in the table.

Thanks everyone for your input.

I still have no idea why it wouldn't set the button.

Jarad answered 23/7, 2010 at 14:22 Comment(1)
I have the same problem, but my button is not in a table, just in a <p> on an <asp:Content> control. I use a master page, if this is relevant.Racehorse

© 2022 - 2024 — McMap. All rights reserved.