views:

98

answers:

4

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 give "The DefaultButton of 'pnlHolder' must be the ID of a control of type IButtonControl." as an error.

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

Thanks

A: 

Try setting it to:

cmdSearchJob.ID

The panel will call FindControl to get the Client ID itself

Rhys Godfrey
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.
monkeypushbutton
A: 

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: http://stackoverflow.com/questions/3237377/allow-enter-key-to-login-in-asp-net/3237556#3237556

rick schott
Tried this and got no error but no defaultButton behavior either.
monkeypushbutton
A: 

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?

Kyle B.
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.
monkeypushbutton
A: 

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.

monkeypushbutton