Default Submit button in ASPX or Master Page

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags with Leave a comment on Default Submit button in ASPX or Master Page

How to handle the default submit button in aspx page having contentplaceholder that is master page

In ASPX page, if there is only one button one which postback is done then there is no problem. However in a case when there are lots of button in page the how you will tell aspx page that which button’s event is default?

To solve that we have to use the “defaultButton” attribute of the “form” tag as shown in below code snap:

<form id="form1" runat="server" defaultbutton = "imgSearch" >
<asp:ImageButton Visible="true" ID="imgSearch" runat="server"/>
<asp:ImageButton Visible="true" ID="imgDelete" runat="server"/>

In above code, the string value supplied to argument “DefaultButton” is the ID of the button control which should made default.

Question : I am getting error “Defaultbutton should be the ID of a control of type IButtonControl” in MasterPage type of application.
Answer : If you try to implement the same logic in “ContentPlaceHolder” type of application where the form tag is at master page, it is the chance that you receive above error.
You have to use the “UniqueId” property of the button control as shown in below C# code.

((DefaultMaster)this.Page.Master).parentForm.DefaultButton = imgSearch.UniqueID;

In above code snap,

  • DefaultMaster is the name of the Master Page.
  • ParentForm is the public property defined on master page which returns the “HtmlForm” control of the page, so that it can be referred in child page.
  • imgSearch is the imagebutton control.

Related posts