CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to Set DefaultButton and DefaultFocus when Using MasterPage in ASP.Net ?
Submitted By Satheesh Babu B
On 7/5/2010 12:13:17 AM
Tags: ASP.Net,CodeDigest  

How to Set DefaultButton and DefaultFocus when Using MasterPage in ASP.Net ?

 

From ASP.Net 2.0, some of the ASP.Net controls have 2 new properties called DefaultButton and DefaultFocus. Setting this property like below will make the Button1 as default button for the page and TextBox1 will be focussed by default.

 

protected void Page_Load(object sender, EventArgs e)

Page.Form.DefaultButton = Button1.ClientID;
Page.Form.DefaultFocus = TextBox1.ClientID;
}

 

But the above code will throw the below error when you have a master page associated with your page.

The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.

 

To reslove this error, you have to change the above code to make the DefaultButton property to UniqueID. It is because, ASP.Net will understand only the UniqueID in order to raise the control's event. Hence, the below code should do.

 

protected void Page_Load(object sender, EventArgs e)

Page.Form.DefaultButton = Button1.UniqueID;
Page.Form.DefaultFocus = TextBox1.ClientID;
}

 

Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..