How to avoid InvalidOperationException when setting the defaultbutton in ASPX content

馋奶兔 提交于 2019-12-08 06:36:07

问题


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.


回答1:


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?




回答2:


Try setting it to:

cmdSearchJob.ID

The panel will call FindControl to get the Client ID itself




回答3:


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?




回答4:


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.



来源:https://stackoverflow.com/questions/3313076/how-to-avoid-invalidoperationexception-when-setting-the-defaultbutton-in-aspx-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!